gpt4 book ai didi

wpf - 为画笔创建 WPF ValueConverter

转载 作者:行者123 更新时间:2023-12-01 12:01:26 26 4
gpt4 key购买 nike

关于Nerd Plus Art今天的博客,有一篇关于为箭头创建WPF资源的帖子,作者经常使用。我有一个带有后退和前进按钮的辅助项目,所以我认为向左和向右箭头在这些按钮上效果很好。

我将 LeftArrowRightArrow 几何图形添加到我的应用程序资源中,然后将它们用作按钮的内容:

<Application x:Class="Notes.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Views/MainWindow.xaml">
<Application.Resources>
<Geometry x:Key="RightArrow">M0,0 L1,0.5 0,1Z</Geometry>
<Geometry x:Key="LeftArrow">M0,0.5 L1,1 1,0Z</Geometry>
</Application.Resources>
</Application>

<Button x:Name="BackButton"
Padding="5,5,5,5"
Command="{x:Static n:Commands.GoBackCommand}">
<Path Data="{StaticResource LeftArrow}" Width="10" Height="8"
Stretch="Fill" Fill="Black"/>
</Button>
<Button x:Name="ForwardButton"
Padding="5,5,5,5"
Command="{x:Static n:Commands.GoForwardCommand}">
<Path Data="{StaticResource RightArrow}" Width="10" Height="8"
Stretch="Fill" Fill="Red" />
</Button>

这行得通,只是无论按钮是否启用,箭头都以黑色绘制。因此,我创建了一个 ValueConverter 以从 bool 转换为 Brush:

class EnabledColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter,
CultureInfo culture)
{
bool b = (bool)value;
return b ? Brushes.Black : Brushes.Gray;
}

public object ConvertBack(object value, Type targetType, object parameter,
CultureInfo culture)
{
throw new NotImplementedException();
}
}

(我意识到我可能应该使用系统颜色而不是硬编码的黑色和灰色,但我只想先让它工作。)

我修改了 PathFill 属性以使用我的转换器(我在应用程序的资源中创建的):

<Path Data="{StaticResource LeftArrow}" Width="10" Height="8"
Stretch="Fill"
Fill="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Button}, Path=IsEnabled, Converter={StaticResource EnabledColorConverter}}"/>

不幸的是,这不起作用,我不确定为什么。当我运行它时,根本没有绘制箭头。我检查了 Visual Studio 中的输出窗口,没有显示任何绑定(bind)错误。我还根据是否应启用按钮验证了 bool 是转换器中的正确值。

如果我将 Path 改回 TextBlock(并以与 Path.Fill 相同的方式绑定(bind)其 Foreground 属性),文本始终以黑色绘制。

我做错了什么吗?为什么我的转换器返回的 Brush 不用于呈现按钮中的 Path

最佳答案

对于这种 UI 状态更新,请尝试使用触发器;它将使您免于编写值转换器,而且编写起来要短得多。

试试这个:

<Application x:Class="Notes.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Views/MainWindow.xaml">
<Application.Resources>
<Geometry x:Key="RightArrow">M0,0 L1,0.5 0,1Z</Geometry>
<Geometry x:Key="LeftArrow">M0,0.5 L1,1 1,0Z</Geometry>

<!-- Base Arrow Style, with a trigger to toggle it Gray when its parent button is disabled -->
<Style x:Key="ArrowStyle" TargetType="Path">
<Setter Property="Width" Value="10"/>
<Setter Property="Height" Value="8"/>
<Setter Property="Stretch" Value="Fill"/>
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Button}, Path=IsEnabled}" Value="False">
<Setter Property="Fill" Value="Gray"/>
</DataTrigger>
</Style.Triggers>
</Style>

<!-- Left Arrow Style, with the Left Arrow fill and data -->
<Style x:Key="LeftArrowStyle" BasedOn="{StaticResource ArrowStyle}" TargetType="Path">
<Setter Property="Fill" Value="Black"/>
<Setter Property="Data" Value="{StaticResource LeftArrow}"/>
</Style>

<!-- Right Arrow Style, with the Right Arrow fill and data -->
<Style x:Key="RightArrowStyle" BasedOn="{StaticResource ArrowStyle}" TargetType="Path">
<Setter Property="Fill" Value="Red"/>
<Setter Property="Data" Value="{StaticResource RightArrow}"/>
</Style>
</Application.Resources>

<Button x:Name="BackButton" Padding="5,5,5,5" IsEnabled="False">
<Path Style="{StaticResource LeftArrowStyle}"/>
</Button>
<Button x:Name="ForwardButton" Padding="5,5,5,5">
<Path Style="{StaticResource RightArrowStyle}"/>
</Button>
</Application>

然后,您在 LeftArrowStyle 和 RightArrowStyle 中分别为左箭头和右箭头设置默认填充。如果您将它设置在 Path 本身上,那么该值将优先并覆盖样式或其触发器可能执行的任何操作。基本样式 ArrowStyle 包含绑定(bind)到父按钮的 DataTrigger - 只要 IsEnabled 为假,它就会触发,并将路径的填充更改为灰色。

关于wpf - 为画笔创建 WPF ValueConverter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/950024/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com