树视图项目选择和鼠标按钮按下事件未触发
Tree View Item Selected and Mouse button down event not firing
我已经为树视图创建了视图模型。还创建了它们的绑定。 I created style trigger so that whenever a tree view item is selected, its background is changed.但是后台没有change.I处理tree view item选中的item和left mouse down事件来查看item是否被选中。但是这两个事件都没有触发。下面是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 事件,但不会在单击鼠标左键时触发。
您可以处理 TreeViewItem
的 PreviewMouseLeftButtonDown
事件:
<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>
private void TreeViewLocalSystem_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
//...
}
我已经为树视图创建了视图模型。还创建了它们的绑定。 I created style trigger so that whenever a tree view item is selected, its background is changed.但是后台没有change.I处理tree view item选中的item和left mouse down事件来查看item是否被选中。但是这两个事件都没有触发。下面是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 事件,但不会在单击鼠标左键时触发。
您可以处理 TreeViewItem
的 PreviewMouseLeftButtonDown
事件:
<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>
private void TreeViewLocalSystem_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
//...
}