Dav*_*itt 3 wpf templates types interface
我有一个IThings的集合,我想HierarchicalDataTemplate为 a创建一个TreeView。直接DataType={x:Type local:IThing}的当然行不通,可能是因为 WPF 创建者不想处理可能的歧义。
由于这应该同时处理IThing来自不同来源的 s,因此引用实现类是不可能的。
现在我正在使用一个 ViewModel,它通过一个具体的实现来代理 IThing:
public interface IThing {
string SomeString { get; }
ObservableCollection<IThing> SomeThings { get; }
// many more stuff
}
public class IThingViewModel
{
public IThing Thing { get; }
public IThingViewModel(IThing it) { this.Thing = it; }
}
<!-- is never applied -->
<HierarchicalDataTemplate DataType="{x:Type local:IThing}">
<!-- is applied, but looks strange -->
<HierarchicalDataTemplate
DataType="{x:Type local:IThingViewModel}"
ItemsSource="{Binding Thing.SomeThings}">
<TextBox Text="{Binding Thing.SomeString}"/>
</HierarchicalDataTemplate>
Run Code Online (Sandbox Code Playgroud)
有没有更好的(即没有代理)方式?
另一种选择(类似于jing的解决方案):如果您只有一种类型的项目,则可以直接设置ItemTemplate。然后您不需要设置键或数据类型。
在您的视图模型中:
public ObservableCollection<IThing> Thingies { get; private set; }
Run Code Online (Sandbox Code Playgroud)
在视图中:
<TreeView ItemsSource="{Binding Thingies}">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding SomeThings}">
<TextBox Text="{Binding SomeString}" />
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2609 次 |
| 最近记录: |