RelativeSource 绑定 HierarchicalDataTemplate 与 collectionviewsource
RelativeSource binding HierarchicalDataTemplate with collectionviewsource
我已经实现了 TreeView
并在视图模型中使用绑定到 ObserableCollection
的 CollectionViewSource
填充了它。
(代码如下)
谁能帮我理解如何将复选框 IsChecked 属性 绑定到树视图第二层集合的 ViewModel 上的 属性。
我正在尝试创建一种情况,如果子项目被选中,那么父节点也会被选中,反之亦然。
我认为主要问题是我不知道如何操作项目,除非它们在叶节点上,否则我无法访问父级中的项目集合..
还有一种方法可以绑定 CollectionViewSource 的源并使用要绑定的项目吗?
感谢任何提示或代码示例
CollectionViewSource
<CollectionViewSource x:Key="CSV"
Source="{Binding TestApplications}">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="BaseAppName" />
<scm:SortDescription PropertyName="Category" />
<scm:SortDescription PropertyName="AppName" />
</CollectionViewSource.SortDescriptions>
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="BaseAppName" />
<PropertyGroupDescription PropertyName="Category" />
</CollectionViewSource.GroupDescriptions>
数据模板
<DataTemplate x:Key="AppNameTemplate">
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding Path=IsChecked, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
IsEnabled="True"
IsThreeState="False"
Name="btnChecked">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Checked">
<i:InvokeCommandAction Command="{Binding RelativeSource= {RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path= DataContext.SelectedTestAppChangedCommand}"/>
</i:EventTrigger>
<i:EventTrigger EventName="Unchecked">
<i:InvokeCommandAction Command="{Binding RelativeSource= {RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path= DataContext.SelectedTestAppChangedCommand2}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</CheckBox>
<TextBlock Text="{Binding AppName}"
FontWeight="Bold">
</TextBlock>
</StackPanel>
</DataTemplate>
<HierarchicalDataTemplate x:Key="CategoryTemplate"
ItemsSource="{Binding Path=Items}"
ItemTemplate="{StaticResource AppNameTemplate}">
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding RelativeSource= {RelativeSource Mode=FindAncestor, AncestorType={x:Type TreeViewItem},AncestorLevel=2}, Path=IsChecked, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
IsEnabled="True"
IsThreeState="False"
Name="btnChecked">
</CheckBox>
<TextBlock Text="{Binding Name}"
FontStyle="Italic">
</TextBlock>
</StackPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate x:Key="BaseAppTemplate"
ItemsSource="{Binding Path=Items}"
ItemTemplate="{StaticResource CategoryTemplate}">
<TextBlock Text="{Binding Name}" />
</HierarchicalDataTemplate>
树视图
treeview
具有以下来源和模板:
ItemsSource="{Binding Source={StaticResource CSV}, Path=Groups}"
ItemTemplate="{StaticResource BaseAppTemplate}"
更多详情
ReadOnly Property TestApplications As ObservableCollection(Of ToolBaseModel)
** 基础模型**
Public Class ToolBaseModel
Inherits NotificationObject
Public Property Key As String
Get
Return mstrKey
End Get
Set(value As String)
mstrKey = value
Me.RaisePropertyChanged(Function() Me.AppName)
End Set
End Property
Public Property Value As String
Get
Return mstrValue
End Get
Set(value As String)
mstrValue = value
Me.RaisePropertyChanged(Function() Me.Value)
End Set
End Property
Public Property BaseAppName As String
Get
Return mstrConfgiFileName
End Get
Set(value As String)
mstrConfgiFileName = value
Me.RaisePropertyChanged(Function() Me.BaseAppName)
End Set
End Property
Public Property Category As String
Get
Return mstrKey.Split(":"c).First
End Get
Set(value As String)
mstrCategory = value
Me.RaisePropertyChanged(Function() Me.Category)
End Set
End Property
Public Property IsChecked() As Boolean
Get
Return mblnIsChecked
End Get
Set(ByVal value As Boolean)
Me.RaisePropertyChanged(Function() Me.IsChecked)
End Set
End Property
Public Property AppName As String
Get
Return mstrKey.Split(":"c)(1)
End Get
Set(value As String)
mstrKey = value
Me.RaisePropertyChanged(Function() Me.AppName)
End Set
End Property
Public Property IsSelected() As Boolean
Get
Return mblnIsSelected
End Get
Set(ByVal value As Boolean)
mblnIsSelected = value
IsChecked = mblnIsSelected
Me.RaisePropertyChanged(Function() Me.IsSelected)
End Set
End Property
EndClass
二级模板的绑定好像不太对
查看您的树的此表示:
--BaseAppTemplate
----CategoryTemplate {Binding ... AncestorLevel=2} 不好!
--------AppNameTemplate {Binding IsChecked} 好!
你看,AncestorLevel=n会向上查找绑定项的第n个父项。
检查树结构,在 CategoryTemplate 之上没有 TreeViewItem 2 级别,因此绑定可能失败(尝试在输出 Windows 上调试那个)
好吧,那是你的 DataTemplate 上的一个错误,但我仍然不明白你想要完成什么,所以如果你能提供一个好的视觉样本(比如我画的那棵蹩脚的树)它会很棒。
我已经实现了 TreeView
并在视图模型中使用绑定到 ObserableCollection
的 CollectionViewSource
填充了它。
(代码如下)
谁能帮我理解如何将复选框 IsChecked 属性 绑定到树视图第二层集合的 ViewModel 上的 属性。
我正在尝试创建一种情况,如果子项目被选中,那么父节点也会被选中,反之亦然。
我认为主要问题是我不知道如何操作项目,除非它们在叶节点上,否则我无法访问父级中的项目集合..
还有一种方法可以绑定 CollectionViewSource 的源并使用要绑定的项目吗?
感谢任何提示或代码示例
CollectionViewSource
<CollectionViewSource x:Key="CSV"
Source="{Binding TestApplications}">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="BaseAppName" />
<scm:SortDescription PropertyName="Category" />
<scm:SortDescription PropertyName="AppName" />
</CollectionViewSource.SortDescriptions>
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="BaseAppName" />
<PropertyGroupDescription PropertyName="Category" />
</CollectionViewSource.GroupDescriptions>
数据模板
<DataTemplate x:Key="AppNameTemplate">
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding Path=IsChecked, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
IsEnabled="True"
IsThreeState="False"
Name="btnChecked">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Checked">
<i:InvokeCommandAction Command="{Binding RelativeSource= {RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path= DataContext.SelectedTestAppChangedCommand}"/>
</i:EventTrigger>
<i:EventTrigger EventName="Unchecked">
<i:InvokeCommandAction Command="{Binding RelativeSource= {RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path= DataContext.SelectedTestAppChangedCommand2}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</CheckBox>
<TextBlock Text="{Binding AppName}"
FontWeight="Bold">
</TextBlock>
</StackPanel>
</DataTemplate>
<HierarchicalDataTemplate x:Key="CategoryTemplate"
ItemsSource="{Binding Path=Items}"
ItemTemplate="{StaticResource AppNameTemplate}">
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding RelativeSource= {RelativeSource Mode=FindAncestor, AncestorType={x:Type TreeViewItem},AncestorLevel=2}, Path=IsChecked, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
IsEnabled="True"
IsThreeState="False"
Name="btnChecked">
</CheckBox>
<TextBlock Text="{Binding Name}"
FontStyle="Italic">
</TextBlock>
</StackPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate x:Key="BaseAppTemplate"
ItemsSource="{Binding Path=Items}"
ItemTemplate="{StaticResource CategoryTemplate}">
<TextBlock Text="{Binding Name}" />
</HierarchicalDataTemplate>
树视图
treeview
具有以下来源和模板:
ItemsSource="{Binding Source={StaticResource CSV}, Path=Groups}"
ItemTemplate="{StaticResource BaseAppTemplate}"
更多详情
ReadOnly Property TestApplications As ObservableCollection(Of ToolBaseModel)
** 基础模型**
Public Class ToolBaseModel
Inherits NotificationObject
Public Property Key As String
Get
Return mstrKey
End Get
Set(value As String)
mstrKey = value
Me.RaisePropertyChanged(Function() Me.AppName)
End Set
End Property
Public Property Value As String
Get
Return mstrValue
End Get
Set(value As String)
mstrValue = value
Me.RaisePropertyChanged(Function() Me.Value)
End Set
End Property
Public Property BaseAppName As String
Get
Return mstrConfgiFileName
End Get
Set(value As String)
mstrConfgiFileName = value
Me.RaisePropertyChanged(Function() Me.BaseAppName)
End Set
End Property
Public Property Category As String
Get
Return mstrKey.Split(":"c).First
End Get
Set(value As String)
mstrCategory = value
Me.RaisePropertyChanged(Function() Me.Category)
End Set
End Property
Public Property IsChecked() As Boolean
Get
Return mblnIsChecked
End Get
Set(ByVal value As Boolean)
Me.RaisePropertyChanged(Function() Me.IsChecked)
End Set
End Property
Public Property AppName As String
Get
Return mstrKey.Split(":"c)(1)
End Get
Set(value As String)
mstrKey = value
Me.RaisePropertyChanged(Function() Me.AppName)
End Set
End Property
Public Property IsSelected() As Boolean
Get
Return mblnIsSelected
End Get
Set(ByVal value As Boolean)
mblnIsSelected = value
IsChecked = mblnIsSelected
Me.RaisePropertyChanged(Function() Me.IsSelected)
End Set
End Property
EndClass
二级模板的绑定好像不太对
查看您的树的此表示:
--BaseAppTemplate
----CategoryTemplate {Binding ... AncestorLevel=2} 不好!
--------AppNameTemplate {Binding IsChecked} 好!
你看,AncestorLevel=n会向上查找绑定项的第n个父项。 检查树结构,在 CategoryTemplate 之上没有 TreeViewItem 2 级别,因此绑定可能失败(尝试在输出 Windows 上调试那个)
好吧,那是你的 DataTemplate 上的一个错误,但我仍然不明白你想要完成什么,所以如果你能提供一个好的视觉样本(比如我画的那棵蹩脚的树)它会很棒。