【问题标题】:Tree View IsSelected with hierarchical data template树视图 IsSelected 与分层数据模板
【发布时间】:2018-09-12 05:35:34
【问题描述】:

我发现了几个在视图模型中绑定 IsSelected 属性的示例。然而,这些都没有处理带有分层数据模板的 TreeView。

我的层次结构是这样的

  • VM_Part
    • VM_Step
    • VM_Step
    • VM_Step
  • VM_Part
    • VM_Step
    • VM_Step

我希望能够在一个部件下选择多个 VM_Part 实例或多个 VM_Steps。我的想法是我可以有一个上下文菜单并对所选项目执行各种命令

<Window x:Class="NameSpace1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:Hipot_Sequence_Editor"
    xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"

    mc:Ignorable="d"
    Title="MainWindow" Height="677.538" Width="896.456">

<Window.DataContext>
    <local:VM_Main></local:VM_Main>
</Window.DataContext>
<Grid>
    <TreeView x:Name="treeView" Grid.Column="1" HorizontalAlignment="Left" Height="628" Margin="10.2,10,0,0" VerticalAlignment="Top" Width="237" Grid.RowSpan="2" ItemsSource="{Binding Parts}">
        <TreeView.Resources>
            <HierarchicalDataTemplate DataType="{x:Type viewModels:VM_Part}" ItemsSource="{Binding VM_Steps}">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding SequenceNumber}" />
                    <TextBlock Text=" - "></TextBlock>
                    <TextBlock Text="{Binding PartNumber}" />
                </StackPanel>
            </HierarchicalDataTemplate>
            <DataTemplate DataType="{x:Type viewModels:VM_Step}">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Name}" />
                </StackPanel>
            </DataTemplate>
        </TreeView.Resources>
    </TreeView>
</Grid>

This 在我看来是我所需要的壁橱示例。我尝试了第一个回答的建议

<TreeView.Resources>
    <Style TargetType="TreeViewItem">
        <Setter Property="IsSelected"
                Value="{Binding Path=IsSelected, Mode=TwoWay}" />
    </Style>
</TreeView.Resources>

但是,此代码似乎假定 IsSelected 位于 VM_Main 而不是 VM_Part 或 VM_Step 中

【问题讨论】:

  • 我刚刚尝试了您发布的解决方案,它对我有用。如果您的项目的数据上下文不是您所期望的,那么肯定有其他事情发生。您如何确认VM_Main 中项目的数据上下文?另外,Parts 属性在VM_Main 中是什么样子的?您的 xaml 似乎还引用了一个名为 viewModels 的命名空间,该命名空间并未出现在您共享的代码中。

标签: wpf mvvm treeview


【解决方案1】:

层次结构中的每个 TreeViewItem

  • VM_Part 树视图项
    • VM_Step TreeViewItem
    • VM_Step TreeViewItem
    • VM_Step TreeViewItem

拥有自己的 DataContext(VM_Part 或 VM_Step)

所以如果 VM_PartVM_Step 具有 IsSelected 属性,则 TreeViewItem 的样式定义正确

<Style TargetType="TreeViewItem">
    <Setter Property="IsSelected"
            Value="{Binding Path=IsSelected, Mode=TwoWay}" />
</Style>

然而,TreeView 中的多选可能会更简单,因为 CheckBoxes 添加到项目模板并绑定到视图模型 IsSelected 属性:

<HierarchicalDataTemplate DataType="{x:Type viewModels:VM_Part}" ItemsSource="{Binding VM_Steps}">
    <StackPanel Orientation="Horizontal">
        <CheckBox IsChecked="{Binding IsSelected, UpdateSourceTrigger=PropertyChanged}" />
        <TextBlock Text="{Binding SequenceNumber}" />
        <TextBlock Text=" - "/>
        <TextBlock Text="{Binding PartNumber}" />
    </StackPanel>
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type viewModels:VM_Step}">
    <StackPanel Orientation="Horizontal">
        <CheckBox IsChecked="{Binding IsSelected, UpdateSourceTrigger=PropertyChanged}" />
        <TextBlock Text="{Binding Name}" />
    </StackPanel>
</DataTemplate>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-03
    相关资源
    最近更新 更多