1

我有两个简单的 ViewModel,NodeViewModel它们LeafViewModel可以是 TreeView 中的项目。就像下面一样。模板被隐式应用,因为我不想要自定义模板选择器。

<UserControl x:Class="MyProject.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:ViewModels="clr-namespace:MyProject.ViewModels" mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300" d:DataContext="{d:DesignData /SampleData/NodeViewModelSampleData.xaml}">
 <UserControl.Resources>
  <HierarchicalDataTemplate DataType="{x:Type ViewModels:NodeViewModel}" ItemsSource={Binding Children}>
   <StackPanel Orientation="Horizontal">
    <CheckBox Content="{Binding Name}" IsChecked="{Binding Result}"/>
   </StackPanel>
  </HierarchicalDataTemplate>
  <HierarchicalDataTemplate DataType="{x:Type ViewModels:LeafViewModel}">
   <StackPanel Orientation="Horizontal">
    <TextBlock Text="{Binding Name}" />
   </StackPanel>
  </HierarchicalDataTemplate>
 </UserControl.Resources>
 <TreeView ItemsSource="{Binding Children}" />
</UserControl>

如何在包含NodeViewModels 和LeafViewModels 的树的混合中生成示例数据,然后在仍然使用隐式模板选择的同时将其显示为树视图中的数据?

4

2 回答 2

1

在没有使用某种模拟框架的情况下,我发现最简单的方法就是将一个生成我的视图模型实例的类组合在一起,并将其用作 Blend 中的数据源。

我突然想到,在 XAML 中定义测试数据可能更容易,尽管这取决于设计允许这样做的视图模型类(例如,使用无参数构造函数和ContentProperty属性等)。

于 2010-11-19T00:12:08.860 回答
0

我认为答案很简单:你不能。

Blend 并不适用于隐式数据模板和模板选择器。这不仅适用于样本数据,也适用于所见即所得的模板编辑。因此,为了可混合性,您应该尽可能避免使用隐含模板和模板选择器。

于 2010-11-30T09:14:09.293 回答