首页 > 解决方案 > 每次鼠标悬停在 WPF 上时调用转换器的 Convert() 方法

问题描述

尽管源中没有值更改,但我想调用转换器的 Convert 方法。现在它只是第一次调用。有什么办法可以做到这一点?

我知道转换方法不会每次都调用,除非源有变化。

 public class ElementNameToDrawingBrushConverter : IValueConverter
    {
        /// <summary>
        /// Method converts colors to brush.
        /// </summary>
        /// <param name="value">value to convert</param>
        /// <param name="targetType">target type</param>
        /// <param name="parameter">param for convert</param>
        /// <param name="culture">culture info instance</param>
        /// <returns>converted brush</returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            DrawingBrush settedBrush = null;
            var shape = value as Shape;

            if (shape == null || parameter == null)
            {
                return value;
            }

            settedBrush = shape.Fill as DrawingBrush;
            return GetIconBrush(settedBrush, parameter as Brush);
        }

        /// <summary>
        /// This method is to provide the new brush for the image
        /// </summary>
        /// <param name="settedBrush">settedBrush</param>
        /// <param name="brushColor">brushColor</param>
        /// <returns>DrawingBrush</returns>
        public static DrawingBrush GetIconBrush(DrawingBrush settedBrush, Brush brushColor)
        {
            DrawingBrush newBrush = null;

            if (settedBrush != null && brushColor != null)
            {
                newBrush = settedBrush.Clone();

                foreach (var geometry in ((DrawingGroup)newBrush.Drawing).Children)
                {
                    GeometryDrawing geometryDrawing = geometry as GeometryDrawing;

                    if (geometryDrawing != null)
                    {
                        if (geometryDrawing.Pen != null)
                        {
                            if (geometryDrawing.Pen.Brush != null)
                            {
                                geometryDrawing.Pen.Brush = brushColor;
                            }
                        }
                        else if (geometryDrawing.Brush != null)
                        {
                            geometryDrawing.Brush = brushColor;
                        }
                    }

                    DrawingGroup drawingGroup = geometry as DrawingGroup;

                    if (drawingGroup != null)
                    {
                        foreach (var geometryInner in drawingGroup.Children)
                        {
                            GeometryDrawing geometryDrawingChild = geometryInner as GeometryDrawing;

                            if (geometryDrawingChild != null)
                            {
                                if (geometryDrawingChild.Pen != null)
                                {
                                    geometryDrawingChild.Pen.Brush = brushColor;
                                }
                                else if (geometryDrawingChild.Brush != null)
                                {
                                    geometryDrawingChild.Brush = brushColor;
                                }
                            }
                        }
                    }
                }
                return newBrush;
            }

            return settedBrush;

        }

        /// <summary>
        /// Convert back implementation
        /// </summary>
        /// <param name="value">value to convert back</param>
        /// <param name="targetType">target type</param>
        /// <param name="parameter">param for convert</param>
        /// <param name="culture">culture info instance</param>
        /// <returns>fallback object</returns>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
             throw new NotImplementedException();
        }
    }

<Button Template="{StaticResource CustomButton}" Content="{Binding MyContent}" ImageContent="{StaticResource  SystemResetNormal}"/>

<ControlTemplate TargetType="{x:Type Button}" x:Key="CustomButton">
        <Border Background="Pink" Height="80" Width="70">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="60"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <Rectangle x:Name="buttonRectangle" Fill="{TemplateBinding ImageContent}" Height="60" Width="40" Loaded="Rectangle_Loaded" />
                <TextBlock x:Name="ContentTxt" Text="{TemplateBinding Content}" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center"/>
            </Grid>
        </Border>
        <ControlTemplate.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter TargetName="buttonRectangle" Property="Fill" Value="{Binding ElementName=buttonRectangle,
                    Converter={StaticResource ElementNameToBrushConverter},
                    ConverterParameter={StaticResource BAquamarine}}" />
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

每次当我将鼠标悬停在它上面时,我都需要调用 Convert 方法。

标签: wpfwpf-controlsivalueconverter

解决方案


尝试绑定到ImageContent而不是buttonRectangle

<Setter TargetName="buttonRectangle" Property="Fill" Value="{Binding ImageContent,
                Converter={StaticResource ElementNameToBrushConverter},
                ConverterParameter={StaticResource BAquamarine}}" />

然后,只要您设置了正确实现接口的ImageContent属性,就应该调用转换器。INotifyPropertyChanged

在您的转换器中,您应该将实际值转换为 aBrush而不是 a Shape

var settleBrush = value as DrawingBrush;
...

推荐阅读