C# 枚举上的datatrigger更改图像
我有一个带有固定背景图像的按钮,我想在上面显示一个小的叠加图像。选择哪个覆盖图像取决于相应viewmodel的依赖项属性(C# 枚举上的datatrigger更改图像,c#,wpf,C#,Wpf,我有一个带有固定背景图像的按钮,我想在上面显示一个小的叠加图像。选择哪个覆盖图像取决于相应viewmodel的依赖项属性(LapCounterPingStatus) 到目前为止,我得到的是: <Button> <Grid> <Image Stretch="None"> <!-- Background Image --> <Image.Style> <S
LapCounterPingStatus
)
到目前为止,我得到的是:
<Button>
<Grid>
<Image Stretch="None"> <!-- Background Image -->
<Image.Style>
<Style TargetType="{x:Type Image}">
<Setter Property="Source" Value="/Images/Pingn.png"/>
</Style>
</Image.Style>
</Image>
<Image Stretch="None" Panel.ZIndex="1"> <!-- Small Overlay Image -->
<Image.Style>
<Style TargetType="{x:Type Image}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=LapCounterPingStatus}" Value="PingStatus.PING_UNKNOWN">
<Setter Property="Source" Value="/Images/RefreshOverlayn.png"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=LapCounterPingStatus}" Value="PingStatus.PING_FAILURE">
<Setter Property="Source" Value="/Images/ErrorOverlayn.png"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=LapCounterPingStatus}" Value="PingStatus.PING_SUCCESS">
<Setter Property="Source" Value="/Images/CheckmarkOverlayn.png"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
</Grid>
</Button>
现在,根本不显示覆盖图像。可能有什么问题?
更新 我的IDE的跟踪窗口显示
System.ArgumentException
和System.FormatException
。
问题源是否可能是未知类型的枚举
PingStatus
im-the-XAML?要使其正常工作,需要两件事:
1-将XAML文件根元素中的xmlns
引用添加到定义枚举的命名空间中:
<UserControl ...
xmlns:my="clr-namespace:YourEnumNamespace;assembly=YourAssembly">
请注意,枚举类型必须以上面定义的xmlns前缀作为前缀
编辑:
如果枚举是在类中声明的,则需要使用以下语法:
{x:Static namespace:ClassName+EnumName.EnumValue}
例如:
{x:Static my:ConfigurationViewModel+PingStatus.PING_UNKNOWN}
WPF+MVVM的完整工作示例
在MSVC 2017上测试
<TextBlock Text="Some text to be colored by an enum">
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}">
<Style.Triggers>
<DataTrigger Binding="{Binding StatusIcon}" Value="Warning">
<Setter Property="Foreground" Value="Yellow"/>
</DataTrigger>
<DataTrigger Binding="{Binding StatusIcon}" Value="Error">
<Setter Property="Foreground" Value="Red}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
他认为:
<TextBlock Text="Some text to be colored by an enum">
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}">
<Style.Triggers>
<DataTrigger Binding="{Binding StatusIcon}" Value="{x:Static my:StatusIcon.Warning}">
<Setter Property="Foreground" Value="Yellow"/>
</DataTrigger>
<DataTrigger Binding="{Binding StatusIcon}" Value="{x:Static my:StatusIcon.Error}">
<Setter Property="Foreground" Value="Red}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
以及VIE模型:
public enum StatusIcon
{
Debug,
Info,
Warning,
Error
}
public class MyViewModel
{
public StatusIcon StatusIcon { get; }
}
我们还使用
Fody
进行自动绑定。您只需将枚举值设置为DataTrigger值。。。在MSVC 2017上测试
<TextBlock Text="Some text to be colored by an enum">
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}">
<Style.Triggers>
<DataTrigger Binding="{Binding StatusIcon}" Value="Warning">
<Setter Property="Foreground" Value="Yellow"/>
</DataTrigger>
<DataTrigger Binding="{Binding StatusIcon}" Value="Error">
<Setter Property="Foreground" Value="Red}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
相关:虽然这家伙已经让它工作了。我添加了xmlns
如下:xmlns:local=“clr namespace:MyCompany.Testbench”
和类似的触发器
。否我收到错误找不到类型“PingStatus”
枚举PingStatus
是在类MyCompany.TestBench.ConfigurationViewModel
中定义的。我必须在某个地方添加类名吗?谢谢。我在任何地方都找不到嵌套类型的语法。“+”语法记录在哪里?我在MSDN或WPF书籍中找不到它。我认为它应该在中,但不是。@skst+符号将包含类型与嵌套命名空间区分开来<代码>类型t=typeof(System.Environment.SpecialFolder);Console.WriteLine(t.FullName);//打印System.Environment+SpecialFolder@skst您指的是Fody的PropertyChanged项目吗?
public enum StatusIcon
{
Debug,
Info,
Warning,
Error
}
public class MyViewModel
{
public StatusIcon StatusIcon { get; }
}
<TextBlock Text="Some text to be colored by an enum">
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}">
<Style.Triggers>
<DataTrigger Binding="{Binding StatusIcon}" Value="Warning">
<Setter Property="Foreground" Value="Yellow"/>
</DataTrigger>
<DataTrigger Binding="{Binding StatusIcon}" Value="Error">
<Setter Property="Foreground" Value="Red}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>