每次将鼠标悬停在 WPF 上时调用转换器的 Convert() 方法

Calling converter's Convert() method everytime on mouse over 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 方法。

尝试绑定到 ImageContent 而不是 buttonRectangle:

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

只要您正确实现了 INotifyPropertyChanged 接口,只要您设置 ImageContent 属性,转换器就会被调用。

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

var settleBrush = value as DrawingBrush;
...