如何仅使用 XAML 显示不同的 Enum 图标?

发布于 2024-09-01 04:34:13 字数 824 浏览 2 评论 0原文

我想根据枚举值显示不同的图标/图像。例如,如果我有以下枚举:

  public enum UploadStatus
  {
      Unknown = 0,
      WaitingToUpload = 10,
      Uploading = 20,
      Uploaded = 30,
      UploadFailed = 40
  }

我想编写看起来像这样的 XAML:

...

<EnumImage Value="{Binding Path=CurrentStatus}">
  <EnumImageItem Value="Unknown"         Image="/images/unknown.png" />
  <EnumImageItem Value="WaitingToUpload" Image="/images/clock.png" />
  <EnumImageItem Value="Uploading"       Image="/images/upload.png" />
  <EnumImageItem Value="Uploaded"        Image="/images/tick.png" />
  <EnumImageItem Value="UploadFailed"    Image="/images/error.png" />
</EnumImage>

...

我发现很多帖子建议自定义 IValueConverters,但这些解决方案不适合 XAML 范例。

有什么指示或建议吗?

I want to show a different icon/image depending on an enum value. For example, if I had the following enum:

  public enum UploadStatus
  {
      Unknown = 0,
      WaitingToUpload = 10,
      Uploading = 20,
      Uploaded = 30,
      UploadFailed = 40
  }

I'd like to write XAML that looks something like this:

...

<EnumImage Value="{Binding Path=CurrentStatus}">
  <EnumImageItem Value="Unknown"         Image="/images/unknown.png" />
  <EnumImageItem Value="WaitingToUpload" Image="/images/clock.png" />
  <EnumImageItem Value="Uploading"       Image="/images/upload.png" />
  <EnumImageItem Value="Uploaded"        Image="/images/tick.png" />
  <EnumImageItem Value="UploadFailed"    Image="/images/error.png" />
</EnumImage>

...

I've found many posts suggesting custom IValueConverters, but those solutions don't fit the XAML paradigm.

Any pointers or suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清醇 2024-09-08 04:34:13

这是一个值转换器,它维护“XAML 范例”,即枚举值和图像之间的关系在 XAML 中维护。

[ContentProperty("Items")]
public class EnumToObjectConverter : IValueConverter
{
    public ResourceDictionary Items { get; set; }

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        string key = Enum.GetName(value.GetType(), value);
        return Items[key];
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException("This converter only works for one way binding");
    }
}

请注意,这是非常通用的,它实际上将任何枚举类型的值映射到任何任意对象。这是它在 Xaml 中的用法:-

<Grid.Resources>
  <local:EnumToObjectConverter x:Key="Icons">
    <ResourceDictionary>
 <BitmapImage x:Key="Unknown" UriSource="/images/unknown.png" />
      <BitmapImage x:Key="WaitingToUpload" UriSource="/images/clock.png" />        
      <BitmapImage x:Key="Uploading"       UriSource="/images/upload.png" />        
      <BitmapImage x:Key="Uploaded"        UriSource="/images/tick.png" />        
      <BitmapImage x:Key="UploadFailed"    UriSource="/images/error.png" />        
    </ResourceDictionary>
  </local:EnumToObjectConverter>
</Grid.Resources>

在绑定枚举类型的属性时可以使用此转换器:-

 <Image Source="{Binding Status, Converter={StaticResource Icons}}" />

Here is a Value converter which maintains the "XAML paradigm" that is the relationship between enum values and images is maintained in XAML.

[ContentProperty("Items")]
public class EnumToObjectConverter : IValueConverter
{
    public ResourceDictionary Items { get; set; }

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        string key = Enum.GetName(value.GetType(), value);
        return Items[key];
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException("This converter only works for one way binding");
    }
}

Note that this is very generic it actually maps values of any enum type to any arbitary object. This is what its usage looks like in Xaml:-

<Grid.Resources>
  <local:EnumToObjectConverter x:Key="Icons">
    <ResourceDictionary>
 <BitmapImage x:Key="Unknown" UriSource="/images/unknown.png" />
      <BitmapImage x:Key="WaitingToUpload" UriSource="/images/clock.png" />        
      <BitmapImage x:Key="Uploading"       UriSource="/images/upload.png" />        
      <BitmapImage x:Key="Uploaded"        UriSource="/images/tick.png" />        
      <BitmapImage x:Key="UploadFailed"    UriSource="/images/error.png" />        
    </ResourceDictionary>
  </local:EnumToObjectConverter>
</Grid.Resources>

This converter can be used when binding property of the enum type:-

 <Image Source="{Binding Status, Converter={StaticResource Icons}}" />
对你的占有欲 2024-09-08 04:34:13

您的 EnumImage 可以使用 ImageDataTrigger 进行设置:

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

来源:根据 XAML 中的值显示图像

Your EnumImage can be setup using Image with DataTrigger:

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

Source: Displaying an image based on value in XAML

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文