<Image Width="16">
<Image.Style>
<Style TargetType="{x:Type Image}">
<Style.Triggers>
<DataTrigger Binding="{Binding Value}" Value="False">
<Setter Property="Source" Value="Resources/image1.png"/>
</DataTrigger>
<DataTrigger Binding="{Binding Value}" Value="True">
<Setter Property="Source" Value="Resources/image2.png"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
参考上面的XAML代码,我对如何将其转换为XAML感到困惑,例如数据触发器部分,任何人都有任何想法?
感谢。
答案 0 :(得分:1)
经过一些重构后,我想你需要这个:
Style style = new Style(typeof(Image));
// Style Setter to handle 'false' case
style.Setters.Add(new Setter(Image.SourceProperty, new BitmapImage(new Uri("Resources/image2.png", UriKind.Relative))));
// DataTrigger to handle 'true' case
DataTrigger dataTrigger = new DataTrigger();
dataTrigger.Binding = new Binding("Value");
dataTrigger.Value = true;
dataTrigger.Setters.Add(new Setter(Image.SourceProperty, new BitmapImage(new Uri("Resources/image1.png", UriKind.Relative))));
style.Triggers.Add(dataTrigger);
this.image.Style = style;