我在TreeviewItem中有一个按钮。当我点击按钮时,TreeviewItem将不会被选中。(样式不会改变)
<TreeView Grid.Row="1" Grid.Column="1" Style="{StaticResource TreeViewStyle}">
<TreeViewItem x:Name="Root" Header="Item 1111111111" >
<TreeViewItem Header="Item 1.1">
<TreeViewItem Header="Item 1.1">
<Border HorizontalAlignment="Stretch" BorderThickness="1" BorderBrush="Red">
<ToggleButton Content="Button">
<ToggleButton.Style>
<Style TargetType="ToggleButton">
<Setter Property="Focusable" Value="False"/>
<Setter Property="Cursor" Value="{x:Static Cursors.Hand}" />
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="HorizontalContentAlignment" Value="Left"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ToggleButton">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Margin="{TemplateBinding Padding}"
RecognizesAccessKey="True"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<!--<Setter Ta TargetName="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TreeViewItem}}, Path=x:Name}" Property="Foreground" Value="Red"/>-->
<Setter Property="Foreground" Value="{StaticResource ThemeColorBrush}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ToggleButton.Style>
</ToggleButton>
</Border>
</TreeViewItem>
</TreeViewItem>
</TreeViewItem>
<TreeViewItem Header="Item 2"/>
</TreeView>
我的预期输出是当用户点击TreeviewItem(有按钮)时它应该将treeviewItem作为
Isselected =真
我怎样才能实现这个
答案 0 :(得分:0)
一种可能的方法是向TreeViewItem添加样式触发器,并且每当IsKeyboardFocusWithin = True时,将IsSelected设置为true。为此,您还需要使ToggleButton具有可调焦性。像这样:
<TreeView Grid.Row="1" Grid.Column="1" Style="{StaticResource TreeViewStyle}">
<TreeView.Resources>
<Style TargetType="TreeViewItem">
<Style.Triggers>
<Trigger Property="IsKeyboardFocusWithin" Value="True">
<Setter Property="IsSelected" Value="True" />
</Trigger>
</Style.Triggers>
</Style>
</TreeView.Resources>
<TreeViewItem x:Name="Root" Header="Item 1111111111" >
<TreeViewItem Header="Item 1.1">
<TreeViewItem Header="Item 1.1">
<Border HorizontalAlignment="Stretch" BorderThickness="1" BorderBrush="Red">
<ToggleButton Content="Button">
<ToggleButton.Style>
<Style TargetType="ToggleButton">
<Setter Property="Focusable" Value="True"/>
<Setter Property="Cursor" Value="{x:Static Cursors.Hand}" />
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="HorizontalContentAlignment" Value="Left"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ToggleButton">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Margin="{TemplateBinding Padding}"
RecognizesAccessKey="True"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<!--<Setter Ta TargetName="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TreeViewItem}}, Path=x:Name}" Property="Foreground" Value="Red"/>-->
<Setter Property="Foreground" Value="{StaticResource ThemeColorBrush}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ToggleButton.Style>
</ToggleButton>
</Border>
</TreeViewItem>
</TreeViewItem>
</TreeViewItem>
<TreeViewItem Header="Item 2"/>
</TreeView>