如何仅使用 XAML 显示不同的 Enum 图标?
我想根据枚举值显示不同的图标/图像。例如,如果我有以下枚举:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个值转换器,它维护“XAML 范例”,即枚举值和图像之间的关系在 XAML 中维护。
请注意,这是非常通用的,它实际上将任何枚举类型的值映射到任何任意对象。这是它在 Xaml 中的用法:-
在绑定枚举类型的属性时可以使用此转换器:-
Here is a Value converter which maintains the "XAML paradigm" that is the relationship between enum values and images is maintained in XAML.
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:-
This converter can be used when binding property of the enum type:-
您的
EnumImage
可以使用Image
和DataTrigger
进行设置:来源:根据 XAML 中的值显示图像
Your
EnumImage
can be setup usingImage
withDataTrigger
:Source: Displaying an image based on value in XAML