Click here to Skip to main content
15,878,748 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello guys!
It's a pretty long time since my last WPF code, so this is probably an easy question I just don't know the answer to.
I have a TreeView that contains different types: Milestone, Issue, ProjectTask, etc.
Now dependent on the selected item I want to get an info panel filled.
I thought of something like this:
XML
<StackPanel Orientation="Vertical" Grid.Column="1" DataContext="{Binding ElementName=ProjectTaskTreeView, Path=SelectedItem}">
    <TextBlock Text="{Binding Id}"></TextBlock>
</StackPanel>

This works fine for an Issue, but when I select a MileStone I want to show the property DisplayText instead of Id and another additional TextBlock.
How can I achieve this the "most easy" or "best practice" way?
Is something similar to the following possible?
XML
<StackPanel Orientation="Vertical" Grid.Column="1" DataContext="{Binding ElementName=ProjectTaskTreeView, Path=SelectedItem}">
 <Content DataType="...Issue...">
  <TextBlock Text="{Binding Id}"></TextBlock>
 </Content>
 <Content DataType="...MileStone...">
  <TextBlock Text="{Binding DisplayText}"></TextBlock>
  <TextBlock Text="{Binding AnotherPropertyOfMileStone}"></TextBlock>
 </Content>
</StackPanel>


Thanks for your help! Markus
Posted
Updated 26-Feb-14 22:34pm
v3

1 solution

So, I found a solution for my question here: http://stackoverflow.com/questions/1152128/wpf-databind-to-a-stackpanel-using-datatemplates[^]

Replacing the StackPanel with a ContentPresenter and instead of DataContext using the Content property worked for me.

Probably it will help someone else :)
 
Share this answer
 
v2

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