WPF 使用 DataTemplate 隐式选择模板,但在“List”之外;
在我的项目中,我有 TreeView,它包含各种类型的对象树(所有对象都是同一超类的子类)。
在 TreeView 的右侧,我希望有一个“面板”(目前我只有一个网格),它显示有关树中当前所选对象的信息。我想使用 DataTemplate 来适应此页面上的第二个示例布局和我的“面板”的内容基于子类类型;但是,我找不到合适的容器(因为我不需要列表控件 - 我想根据树视图中的选择更改一个项目的显示)。
这个问题问同样的事情,但我认为答案不适合我,因为我希望模板根据类型动态变化。
即我希望得到类似的东西:
<[A Suitable Container] Margin="189,39,12,12" DataContext="{Binding ElementName=treeView1, Path=SelectedItem}">
<DataTemplate DataType="{x:Type local:subclass1}">
<Grid>
<!-- subclass1 specific stuff -->
</Grid>
</DataTemplate>
<DataTemplate DataType="{x:Type local:subclass2}">
<Grid>
<!-- subclass2 specific stuff -->
</Grid>
</DataTemplate>
</[A Suitable Container]>
In my project, I have TreeView, which contains a tree of objects of various types (all subclassed from the same superclass).
To the right of my TreeView I would like to have a "panel" (at the moment I just have a Grid) which displays information about the object currently selected in the tree. I want to use DataTemplate, as in the second example on this page, to adapt the layout & content of my "panel" based on the subclass type; however, I cannot find a suitable container (as I don't want a list control - I want to change my display for one item based on the selection in the treeview).
This question asks the same thing but I don't think the answer is suitable for me because I want the template to change dynamically depending on the type.
I.e. I was hoping for something like:
<[A Suitable Container] Margin="189,39,12,12" DataContext="{Binding ElementName=treeView1, Path=SelectedItem}">
<DataTemplate DataType="{x:Type local:subclass1}">
<Grid>
<!-- subclass1 specific stuff -->
</Grid>
</DataTemplate>
<DataTemplate DataType="{x:Type local:subclass2}">
<Grid>
<!-- subclass2 specific stuff -->
</Grid>
</DataTemplate>
</[A Suitable Container]>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
ContentControl
Use a
ContentControl