Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3980618
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:18:44+00:00 2026-05-20T05:18:44+00:00

I have a collection of objects bound to a hierarchical data template, each of

  • 0

I have a collection of objects bound to a hierarchical data template, each of my objects have a property on them (lets call it Property “A”) that is of a certain type. This type varies among each of the objects.

If the data template contains an image and some text, what would be the best way to change the image that is displayed in the template based on the type of property “A”.

I know I could just stick this into a converter and do the binding translation manually in code, but with all the binding facilities available in WPF, I think theres probably a better way.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T05:18:45+00:00Added an answer on May 20, 2026 at 5:18 am

    It’s pretty simple to do this within your data template, if you create local data templates and use a ContentPresenter. This template presents objects of type MyObject, displaying an image whose source is determined by the type of the A property next to a TextBlock that displays the content of the Text property:

    <DataTemplate DataType="{x:Type MyObject}">
       <StackPanel Orientation="Horizontal">
          <StackPanel.Resources>
             <DataTemplate DataType="{x:Type Thing1}">
                <Image Source="thing1.png"/>
             </DataTemplate>
             <DataTemplate DataType="{x:Type Thing2}">
                <Image Source="thing2.png"/>
             </DataTemplate>
          </StackPanel.Resources>
          <ContentPresenter Content="{Binding A}"/>
          <TextBlock Text="{Binding Text}"/>
       </StackPanel>
    </DataTemplate>
    

    If you want to use styles to do this instead, you’re going to run into a problem, because data triggers want to look at property values, and the type of the A property is not, itself, exposed as a property.

    Unless, of course, you implement one:

    public Type AType { get { return A.GetType(); } }
    

    (You’ll also need to raise PropertyChanged for AType when the value of A changes.) Once you’ve done this, you should be able to implement a data trigger in a style, e.g.:

    <Style TargetType="Image">
       <Setter Property="Source" Value="default.png"/>
       <Style.Triggers>
          <DataTrigger Binding="{Binding AType}" Value="{x:Type Thing1}">
             <Setter Property="Source" Value="thing1.png"/>
          </DataTrigger>
          <DataTrigger Binding="{Binding AType}" Value="{x:Type Thing2}">
             <Setter Property="Source" Value="thing2.png"/>
          </DataTrigger>
       </Style.Triggers>
    </Style>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ItemsControl that is bound to a collection of objects. Each object
I have a ComboBox that is bound to a collection of CLR objects. Each
I have a collection of objects of the same type, let's call it DataItem
I have a collection of objects (List) that is bound to a datagridview. How
I have a ComboBox with its Items property bound to a collection of objects.
I have a list of objects. Each object has a property named ext that
I have an items control that is bound to a collection of objects. The
We have a data grid on a window that is bound to a collection
I have a ComboBox bound to a collection of objects defined as this. public
I have two Collection objects, I want to associate each object of these two

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.