locked
Data binding (WPF) RRS feed

  • Question

  • Hi,

    I've got a collection of items of a class called MyImage.  Each MyImage item has two properties: Visual and IR, which return bitmaps.  I want to databind a list in WPF to either the Visual or IR property of each element in the collection.  How can I do this?  I'm guessing I just databind with the list itself and then have some kind of formatter that uses the image property to render the bitmap inside the element, or something.

    Thanks.

    • Moved by CoolDadTx Thursday, December 10, 2009 2:49 PM WPF related (From:Visual C# General)
    Thursday, December 10, 2009 12:10 PM

Answers

  • Hi,

    do you want to display only one kind of image at a given time, or do you need a list with mixed images depending on some condition?

    In WPF, you'd just bind to the list itself in an ItemsControl and set the ItemsControl's ItemTemplate to a DataTemplate that has the DataType MyImage. In the ItemTemplate, you'd define an Image element that binds to the Visual or IR property. You could swap the mode for the whole list by changing the ItemsTemplate, I suppose. If you need the images mixed, you can either create a DataTemplateSelector and put it into the ItemsControl's ItemTemplateSelector property, or you can try to create a DataTrigger that exchanges the the template given a condition.
    http://wpfglue.wordpress.com
    • Marked as answer by RobinsonUK Thursday, December 10, 2009 4:57 PM
    Thursday, December 10, 2009 4:28 PM

All replies

  • Hi,

    do you want to display only one kind of image at a given time, or do you need a list with mixed images depending on some condition?

    In WPF, you'd just bind to the list itself in an ItemsControl and set the ItemsControl's ItemTemplate to a DataTemplate that has the DataType MyImage. In the ItemTemplate, you'd define an Image element that binds to the Visual or IR property. You could swap the mode for the whole list by changing the ItemsTemplate, I suppose. If you need the images mixed, you can either create a DataTemplateSelector and put it into the ItemsControl's ItemTemplateSelector property, or you can try to create a DataTrigger that exchanges the the template given a condition.
    http://wpfglue.wordpress.com
    • Marked as answer by RobinsonUK Thursday, December 10, 2009 4:57 PM
    Thursday, December 10, 2009 4:28 PM
  • Hi,

    Thanks for your reply.  Yes, I would like the user to be able to select whether he wants to see the Visual next to the IR, just the visual, or just the IR, so to that extent the item display itself is dynamic.  I see this is a little over my head (in terms of WPF) so some reading on Data Templates and DataTemplateSelector is in order.  Still, I now know where to look.  Thanks.

    Thursday, December 10, 2009 4:48 PM