Click here to Skip to main content
15,886,851 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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!
Posted

1 solution

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[^]
 
Share this answer
 
Comments
paleGegg0 25-Mar-11 8:56am    
IValueConverter did the job! Thanks for the help! My problem is now solved :)
Tarun.K.S 25-Mar-11 9:17am    
Glad to hear that! :)
Good luck!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900