wpf - 每次鼠标悬停在 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}}" />
然后,只要您设置了正确实现接口的ImageContent
属性,就应该调用转换器。INotifyPropertyChanged
在您的转换器中,您应该将实际值转换为 aBrush
而不是 a Shape
:
var settleBrush = value as DrawingBrush;
...
推荐阅读
- reactjs - 使用reduce映射对象列表时过滤
- excel - 排序第 3 - 5 列不影响第 1 列或第 2 列
- javascript - 使用 jQuery (Wordpress) 使整个 div 可点击
- ipfs - IPFS 应用程序在向请求者呈现内容时可以隐藏 CID 吗?
- javascript - 从 api 获取 json 响应 - react.js
- django - 开发基于 Wagtail 的网站的更好环境
- scala - 具有应用功能的数据集和案例类
- bash - unix bash shell 脚本中的延迟匹配
- javascript - 无法发送仅更改 imageUri 和 employeeBio 的 PUT 请求
- reactjs - ReactJS - PropType 设置为数字的数字输入,但被初始化为零