wpf 根据 XAML 中的值显示图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17967416/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:19:33  来源:igfitidea点击:

Displaying an image based on value in XAML

c#wpf

提问by Mahmoud Samy

How can I display an image based on a value in XAML?

如何根据 XAML 中的值显示图像?

I have gender enumeration

我有性别枚举

[DataContract(Name = "Gender")]
public enum GenderEnum
{
    [EnumMember] NotSpecified,
    [EnumMember] Male,
    [EnumMember] Female,
}

At my Model class I have a property of that enumeration type called "Gender". Now I want to display an image based on the value of "Gender" via the XAML side.

在我的 Model 类中,我有一个名为“Gender”的枚举类型的属性。现在我想通过 XAML 端显示基于“性别”值的图像。

回答by Mahmoud Samy

XAML:

XAML:

   <Image Tag="{Binding Gender}" Width="48" Height="48">
      <Image.Style>
        <Style TargetType="Image">
            <Style.Triggers>
                <DataTrigger  Binding="{Binding Gender}" Value="Male">
                    <Setter Property="Source" Value="/Resources/Client_Male.png"/>
                </DataTrigger >
                <DataTrigger  Binding="{Binding Gender}" Value="Female">
                    <Setter Property="Source" Value="/Resources/Client_Female.png"/>
                </DataTrigger >
            </Style.Triggers>
        </Style>
      </Image.Style>
     </Image>