WPF ListBox,数据模板和触发器 [英] WPF ListBox, Datatemplates and triggers

查看:75
本文介绍了WPF ListBox,数据模板和触发器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!

我遇到了以下问题.

我需要构建自定义的列表框,当绑定属性名称和值等时,该列表框可以很好地工作.问题有点儿修饰,但需要.我的数据类
提供具有8个不同值的enumclass,每个值需要在列表框项中显示为不同的图像.我已经包含了相应的图像
到我的项目/images文件夹中,因此任务是将枚举值绑定到正确的图片.我通过使用datatemplates构建列表框.

我尝试使用Datatemplate.triggers来完成任务并设法更改了前景,所以
我以为这可以完成工作,只需修改显示图像的路径即可,但是该怎么做!

Hi all!

I ran following problem.

I need to build customized listbox which works just fine when it comes to binding property names and values etc. The problem is kinda cosmetic but needed. My dataclass
provides enumclass which has 8 diffirent values, each value needs to be presented with different image in listbox item. I have already included corresponding images
into my project /images folder, so task is to bind enum value to right picture. I build my listbox by using datatemplates.

I tried Datatemplate.triggers to do the task and managed to change foreground so
I thought this can do the job, just modify path of presented image, but how to do that!?

.
.
<listbox.itemtemplate>>
  <DataTemplate>
    <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding Path=MyEnumProperty}" Value="SpecifigEnum">
            <Setter Property="ListBoxItem.Foreground" Value="Red"></Setter> //Works fine, but in this section I need to specify image with known path!
        </DataTrigger>
        //Add here triggers for the rest bossible enums
    </DataTemplate.Triggers>
  </DataTemplate>
</ListBox.ItemTemplate>


.
.


我想知道如何解决这个问题,或者我的道路正确吗?如果我的数据类在其中提供了图像路径,则该任务将更容易,但是我无法更改它
图片的决定必须在客户端进行.

我希望你有主意.喜欢听到你的想法! :)

干杯!


.
.


I like to know how to solve this problem or is my path even right one? The task would be easier if my dataclass provides imagepath within, but I cannot change that so
decicion of image must make on client side.

I hope you got the idea. Like to hear your thougts! :)

Cheers!

推荐答案

好吧,我并没有完全理解你的想法,但是就像你说的那样:
我的数据类
提供具有8个不同值的枚举类,每个值需要在列表框项中显示为不同的图像.

这是我的想法:
为此,可以使用实现IValueConverter接口的类.它将使用enum值并返回可以绑定到图像源的BitMapImage.

以下是一些可能对您有帮助的链接:
http://www.codeproject.com/KB/WPF/FriendlyEnums.aspx [ ^ ]

了解IValueConverter [ http://www.codeproject.com/KB/WPF/ScriptValueConverter.aspx [ ^ ]
Well I didn''t completely get your idea, but like you said :
My dataclass
provides enumclass which has 8 diffirent values, each value needs to be presented with different image in listbox item.

Here is my idea :
For this, you can use a class which implements IValueConverter interface. It will take the enum value and return a BitMapImage which can binded to an Image Source.

Here are a few links which might help you :
http://www.codeproject.com/KB/WPF/FriendlyEnums.aspx[^]

Understanding IValueConverter[^]

http://www.codeproject.com/KB/WPF/ScriptValueConverter.aspx[^]


这篇关于WPF ListBox,数据模板和触发器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆