我有一个 TreeView
包含来自不同类的不同对象。现在我想构建一个属性面板,它显示不同的内容,具体取决于在 TreeView
中选择的对象/类。构建此类面板的最佳方法是什么?根据选择不同的面板和折叠面板(这对我来说会更容易实现 ObserverPattern 吗?)?或者其他方法?
最佳答案
我会将属性面板(可能只是一个 ContentControl
)绑定(bind)到 TreeView
中的 SelectedItem
:
<ContentPanel Content="{Binding SelectedItem, ElementName=_treeView}"/>
然后我将使用 DataTemplate
为您拥有的每一类项目显示正确的面板:
<DataTemplate DataType="{x:Type local:SomeClass}">
<Label>This is displayed for SomeClass</Label>
</DataTemplate>
<DataTemplate DataType="{x:Type local:SomeOtherClass}">
<Label>This is displayed for SomeOtherClass</Label>
</DataTemplate>
显然,您的 DataTemplate
可以根据需要显示 TreeView
中存在的各种类的复杂程度。
关于c# - 如何在 WPF 中构建类似 VS 的属性面板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/748027/