我为树视图创建了视图模型。还创建了他们的绑定。我创建了样式触发器,以便每当选择树视图项时,其背景都会更改。但是背景没有改变。我处理了所选择的项目并将鼠标按下了树视图项目的事件以查看该项目是否被选中。但这两件事都没有发生。以下是XAML。
<TreeView Grid.Row="0" x:Name="TreeViewLocalSystem" BorderBrush="Transparent" Panel.ZIndex="0">
<TreeView.ItemContainerStyle>
<Style TargetType="TreeViewItem">
<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="{StaticResource TextBoxBackgroundColor}"></Setter>
</Trigger>
<Trigger Property="IsSelected" Value="False">
<Setter Property="Background" Value="Transparent"></Setter>
</Trigger>
</Style.Triggers>
</Style>
</TreeView.ItemContainerStyle>
<TreeView.Resources>
<HierarchicalDataTemplate ItemsSource="{Binding Path=Children}" DataType="{x:Type local:LocalTreeViewItemModel}">
<TreeViewItem Header="{Binding Path=Text}" Selected="TreeViewItem_Selected" HorizontalAlignment="Left" FontSize="{StaticResource MediumFontSize}" MouseLeftButtonDown="TreeViewItem_MouseLeftButtonDown" />
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
我做错了什么?
编辑:我注意到的另一件事是当我点击右键时会触发MouseDown事件,但是鼠标左键单击时不会触发。
答案 0 :(得分:1)
您可以处理PreviewMouseLeftButtonDown
的{{1}}事件:
TreeViewItem
<TreeView Grid.Row="0" x:Name="TreeViewLocalSystem" BorderBrush="Transparent" Panel.ZIndex="0">
<TreeView.ItemContainerStyle>
<Style TargetType="TreeViewItem">
<EventSetter Event="PreviewMouseLeftButtonDown" Handler="TreeViewLocalSystem_PreviewMouseLeftButtonDown" />
<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="{StaticResource TextBoxBackgroundColor}"></Setter>
</Trigger>
<Trigger Property="IsSelected" Value="False">
<Setter Property="Background" Value="Transparent"></Setter>
</Trigger>
</Style.Triggers>
</Style>
</TreeView.ItemContainerStyle>
<TreeView.Resources>
<HierarchicalDataTemplate ItemsSource="{Binding Path=Children}" DataType="{x:Type local:LocalTreeViewItemModel}">
<TextBlock Text="{Binding Path=Text}" HorizontalAlignment="Left" FontSize="{StaticResource MediumFontSize}" />
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>