使用具有不同类型细节的主 - 详细视图

问题描述 投票:3回答:2

我尝试使用MVVM模式创建一个将在一侧(主)显示树视图的视图,并在左侧(详细信息)显示所选节点的一些信息。

treeview绑定在ViewModel的集合上,ViewModel实际上是一个抽象类。我有两个继承自我的抽象ViewModel的类,一个代表一个类别,另一个代表一个需求。

在树中,类别可能具有类别或要求的子项。

要求不能有孩子,他们只是叶子。

即:

  • 第1类 要求1 子类别1
  • 第2类 子类别2
  • 第3类

我设法在详细视图中显示抽象类中的一些数据。我的问题是,如果选择了类别或要求,我必须显示不同的数据......我不知道如何做到这一点。

是否有一个控件允许我根据树中所选节点的类型显示数据?

我的XAML现在看起来像这样:

<Grid DataContext="{Binding Requirements}">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="350" />
        <ColumnDefinition Width="400*" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition />
    </Grid.RowDefinitions>

    <TreeView 
        x:Name="treeRequirements"
        Grid.Column="0" Grid.Row="0" 
        HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
        ItemsSource="{Binding}">
        <TreeView.ItemContainerStyle>
            <!-- This Style binds a TreeViewItem to a PersonViewModel. -->
            <Style TargetType="{x:Type TreeViewItem}">
                <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
                <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
                <Setter Property="FontWeight" Value="Normal" />
                <Style.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="FontWeight" Value="Bold" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </TreeView.ItemContainerStyle>

        <TreeView.ItemTemplate>
            <HierarchicalDataTemplate ItemsSource="{Binding Children}">
                <TextBlock Text="{Binding Name}" />
            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>
    </TreeView>

    <Grid 
        Grid.Column="1" Grid.Row="0"
        DataContext="{Binding ElementName=treeRequirements, Path=SelectedItem}">
        <Grid.ColumnDefinitions>
            <ColumnDefinition></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>

        <!-- Name comes from the abstract class, so no problem -->
        <TextBlock 
            Grid.Row="0" Grid.Column="0">
            Name
        </TextBlock>
        <TextBox
            Grid.Row="0" Grid.Column="1" 
            Text="{Binding Path=Name, Mode=TwoWay}" />


    </Grid>
</Grid>

我的问题是,我不知道如何根据所选节点所代表的视图模型的类型显示不同的细节。我只能显示抽象类的属性。

有帮助吗?

编辑

总结一下我的问题,整个主 - 细节和树视图与问题无关,只是放在上下文中。我的问题实际上只是根据我的viewmodel的子类型显示不同的字段,这可能会有所不同。

c# .net wpf xaml mvvm
2个回答
0
投票

我很好奇你的问题,所以我做了一些环顾四周。看起来您可能想要使用DataTemplateSelector。有一个很好的例子显示here


2
投票

您需要声明多个HierarchicalDataTemplates作为指定每个DataType属性的资源。如果未指定Treeview.ItemTemplate .net将在运行时选择最佳匹配模板并相应地显示数据。

例:

<TreeView ItemsSource={Binding}>
   <TreeView.Resources>
      <HierarchicalDataTemplate DataType="{x:Type local:Type1}">
      ...
      </HierarchicalDataTemplate>
      <HierarchicalDataTemplate DataType="{x:Type local:Type2}">
      ...
      </HierarchicalDataTemplate>
    </TreeView.Resources>
</TreeView>

您可能还想阅读以下文章(特别是使用HierarchicalDataTemplate的足球示例:http://msdn.microsoft.com/en-us/library/ms742521.aspx

© www.soinside.com 2019 - 2024. All rights reserved.