使用包含不同类型详细信息的主从视图

发布于 2024-10-22 02:03:42 字数 3057 浏览 1 评论 0 原文

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

树视图绑定在 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>

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

有什么帮助吗?

编辑

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

I try to make a view that will display a treeview on one side (master), and for the selected node, some information on the left (detail), using the MVVM pattern.

The treeview is bound on a collection of a ViewModel that is, in fact, an abstract class. I have two classes that inherits from my abstract ViewModel, one representing a category, the other one a requirement.

In the tree, the categories may have childs that are either categories or requirements.

The requirements cannot have childs, they are just leaves.

i.e.:

  • category 1
    • requirement 1
    • sub-category 1
  • category 2
    • sub-category 2
  • category 3

I managed to display some data from the abstract class in my detail view. My problem is that I have to display different data if a category or a requirement is selected... I have no clue how to do this.

Is there a control that will allow me to display data based on the type of the selected node from my tree?

My XAML looks like this for now:

<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>

My problem is, I have no idea how to display different details based on the type of the viewmodel represented by the selected node. I am only able to display properties of the abstract class.

Any help?

EDIT

To sum up my question, the whole master-detail and treeview are pretty irrelevant to the problem, there are just to put in context. My problem is really just to display different fields based on the subtype of my viewmodel, which may vary.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱人如己 2024-10-29 02:03:42

您需要将多个 HierarchicalDataTemplate 声明为资源,并为每个资源指定 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

You need to declare multiple HierarchicalDataTemplates as resources specifying the DataType attribute for each. If you do not specify a Treeview.ItemTemplate .net will select the best matching template at runtime and display the data accordingly.

Example:

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

You may also want to read the following article (specifically the soccer example which uses HierarchicalDataTemplate: http://msdn.microsoft.com/en-us/library/ms742521.aspx

赴月观长安 2024-10-29 02:03:42

我很好奇你的问题,所以我自己环顾四周。看起来您可能想要使用 DataTemplateSelector。有一个很好的例子 此处

I was curious about your question, so I did some looking around myself. It looks like you might want to use a DataTemplateSelector. There's a nice example shown here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文