WPF:StackPanel中的不同内容取决于DataContext的类型 [英] WPF: Different content in StackPanel dependent on the DataContext's type
问题描述
大家好!
自上次WPF代码以来已经过去了很长时间,所以这可能是一个简单的问题,我只是不知道答案.
我有一个包含不同类型的TreeView:里程碑,问题,ProjectTask 等.
现在,取决于所选的项目,我想填充一个信息面板.
我想到了这样的东西:
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:
<StackPanel Orientation="Vertical" Grid.Column="1" DataContext="{Binding ElementName=ProjectTaskTreeView, Path=SelectedItem}">
<TextBlock Text="{Binding Id}"></TextBlock>
</StackPanel>
这对于问题来说很好用,但是当我选择 MileStone 时,我想显示属性 DisplayText 而不是 Id >和另一个附加的TextBlock.
如何以最简单"或最佳实践"的方式实现这一目标?
是否可能与以下内容类似?
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?
<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>
谢谢你的帮助! Markus
Thanks for your help! Markus
推荐答案
因此,我在这里找到了针对我的问题的解决方案: ^ ]
使用ContentPresenter而不是DataContext替换StackPanel可以使用Content属性为我工作.
可能会对其他人有所帮助:)
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 :)
这篇关于WPF:StackPanel中的不同内容取决于DataContext的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!