从树视图项的项源读取"IsExpanded"属性



所以我希望TreeView项的IsExpanded属性反映在数据上下文中。

<TreeView x:Name="TreeViewMonitorEvents" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Grid.Column="0" Grid.Row="1" Grid.RowSpan="5"
ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=Page}, Path=MonitorEventCatagories}">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type tree:TreeGroup}" ItemsSource="{Binding Members}" >
<StackPanel Orientation="Horizontal" VerticalAlignment="Center" MouseMove="DragDrop_MouseMove_TreeGroup">
<CheckBox Name="CheckboxTreeGroup" IsChecked="{Binding Path=(tree:TreeItemHelper.IsChecked), Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Template="{DynamicResource MonitoringUICheckBox}" Style="{StaticResource MonitoringUICheckBoxStyling}"
MouseMove="DragDrop_MouseMove_TreeGroup" Checked="CheckboxTreeGroup_Checked" Unchecked="CheckboxTreeGroup_Unchecked">
</CheckBox>
<TextBlock Text="{Binding Name}" Style="{StaticResource MonitorUIText}" MouseMove="DragDrop_MouseMove_TreeGroup"/>
</StackPanel>
<HierarchicalDataTemplate.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}"  >
<Setter Property="IsExpanded" Value="{Binding IsExpanded,Mode=TwoWay}" />
</Style>
</HierarchicalDataTemplate.ItemContainerStyle>
</HierarchicalDataTemplate>

即树组类。点击此处查看Treegroup:

namespace RTX64MonitorUtility.TreeView
{
//Data class.  Holds information in the Datacontext section of the UI Elements it is attached to.
public class TreeGroup : DependencyObject, IParent<object>
{

public string Name { get; set; }
public List<TreeMonitoringEvent> Members { get; set; }
public IEnumerable<object> GetChildren()
{
return Members;
}
public bool IsExpanded { get; set; } = false;
}
}

以下是它的绘图列表:

namespace RTX64MonitorUtility.Pages
{
/// <summary>
/// Interaction logic for EventsAndTriggers.xaml
/// </summary>
public partial class EventsAndTriggers : Page
{
public ObservableCollection<TreeGroup> MonitorEventCatagories { get; set; }
...
}
}

我在这里的主要目标是,如果TreeGroup项没有展开,那么孩子的CheckedUnchecked事件就不会被触发,所以我需要为他们做必要的操作。如果我能找到一种从这些事件中读取IsExpanded值的方法,那也是一个解决方案。

您通常使用以项目容器为目标的Style将项目容器的属性(如TreeViewItem.IsExpandedListBoxItem.IsSelected等(绑定到数据模型。项目容器的DataContext是数据模型:

<TreeView>
<TreeView.ItemContainerStyle>
<Style TargetType="TreeViewItem">
<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
</Style>
</TreeView.ItemContainerStyle>
</TreeView>

接下来,正确地实现数据模型。作为数据绑定源的属性必须实现为依赖属性(对于依赖对象(或引发INotifyPropertyChanged.PropertyChanged事件。

由于DependencyObject扩展了DispatcherObject,任何扩展DependencyObject的类都是线程仿射的:在WPF中,DispatcherObject只能由与其关联的Dispatcher访问
因此,在数据模型上,您通常更喜欢INotifyPropertyChanged
DependencyObject用于定义绑定到Dispatcher线程的UI对象。

// Let the model implement the INotifyPropertyChanged interface
public class TreeGroup : INotifyPropertyChanged, IParent<object>
{
// Follow this pattern for all properties that will change 
// and are source of a data binding.
private bool isExpanded;
public IsExpanded 
{ 
get => this.isExpanded; 
set; 
{
this.isExpanded = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string propertyName = "")
=> this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}

请参阅Microsoft文档:数据绑定概述(WPF.NET(

最新更新