WPF TreeView的多个展开按钮
在过去的几年里,我一直在阅读这里的问题和答案,但这是我在找不到我真正需要的东西后的第一个问题 我正在创建一个WPF MVVM应用程序。 在这个应用程序中,我使用一个树视图,其中的每个节点都是两个对象的元组(比如说字符串) 我想添加另一个展开按钮,即节点左侧的原始按钮和节点右侧的另一个按钮 我想在TreeViewItem标题中添加一个按钮,但我不明白(搜索后…)如何将他绑定到扩展操作WPF TreeView的多个展开按钮,wpf,xaml,treeview,Wpf,Xaml,Treeview,在过去的几年里,我一直在阅读这里的问题和答案,但这是我在找不到我真正需要的东西后的第一个问题 我正在创建一个WPF MVVM应用程序。 在这个应用程序中,我使用一个树视图,其中的每个节点都是两个对象的元组(比如说字符串) 我想添加另一个展开按钮,即节点左侧的原始按钮和节点右侧的另一个按钮 我想在TreeViewItem标题中添加一个按钮,但我不明白(搜索后…)如何将他绑定到扩展操作 希望听到一些想法…因此,如果我们去看看x:Key=“{x:Type TreeViewItem}”中的按钮,我们会看
希望听到一些想法…因此,如果我们去看看
x:Key=“{x:Type TreeViewItem}”
中的按钮,我们会看到其中有一个切换按钮
<ToggleButton x:Name="Expander"
Style="{StaticResource ExpandCollapseToggleStyle}"
ClickMode="Press"
IsChecked="{Binding IsExpanded,
RelativeSource={RelativeSource TemplatedParent}}"/>
因此,如果说我们想(根据您的要求)复制它,在同一个模板中,我们可以使用不同的名称完全复制它。喜欢
<Style x:Key="{x:Type TreeViewItem}"
TargetType="{x:Type TreeViewItem}">
<Setter Property="Background"
Value="Transparent" />
<Setter Property="HorizontalContentAlignment"
Value="{Binding Path=HorizontalContentAlignment,
RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" />
<Setter Property="VerticalContentAlignment"
Value="{Binding Path=VerticalContentAlignment,
RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" />
<Setter Property="Padding"
Value="1,0,0,0" />
<Setter Property="Foreground"
Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" />
<Setter Property="FocusVisualStyle"
Value="{StaticResource TreeViewItemFocusVisual}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TreeViewItem}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition MinWidth="19"
Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<!-- ** Let's add a column for it ** -->
<ColumnDefinition MinWidth="19" Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="SelectionStates">
<VisualState x:Name="Selected">
<Storyboard>
<ColorAnimationUsingKeyFrames Storyboard.TargetName="Bd"
Storyboard.TargetProperty="(Panel.Background).
(SolidColorBrush.Color)"
>
<EasingColorKeyFrame KeyTime="0"
Value="{StaticResource SelectedBackgroundColor}" />
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Unselected" />
<VisualState x:Name="SelectedInactive">
<Storyboard>
<ColorAnimationUsingKeyFrames Storyboard.TargetName="Bd"
Storyboard.TargetProperty="(Panel.Background).
(SolidColorBrush.Color)">
<EasingColorKeyFrame KeyTime="0"
Value="{StaticResource SelectedUnfocusedColor}" />
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="ExpansionStates">
<VisualState x:Name="Expanded">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)"
Storyboard.TargetName="ItemsHost">
<DiscreteObjectKeyFrame KeyTime="0"
Value="{x:Static Visibility.Visible}" />
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Collapsed" />
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<ToggleButton x:Name="Expander"
Style="{StaticResource ExpandCollapseToggleStyle}"
ClickMode="Press"
IsChecked="{Binding IsExpanded,
RelativeSource={RelativeSource TemplatedParent}}"/>
<Border x:Name="Bd"
Grid.Column="1"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Padding="{TemplateBinding Padding}">
<ContentPresenter x:Name="PART_Header"
ContentSource="Header"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"/>
</Border>
<ItemsPresenter x:Name="ItemsHost"
Grid.Row="1"
Grid.Column="1"
Grid.ColumnSpan="2"
Visibility="Collapsed" />
<!-- ** We add a duplicate ToggleButton to act as the expander ** -->
<ToggleButton x:Name="Expander2" Grid.Column="3"
Style="{StaticResource ExpandCollapseToggleStyle}"
ClickMode="Press"
IsChecked="{Binding IsExpanded,
RelativeSource={RelativeSource TemplatedParent}}"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="HasItems"
Value="false">
<Setter TargetName="Expander"
Property="Visibility"
Value="Hidden" />
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="HasHeader"
Value="false" />
<Condition Property="Width"
Value="Auto" />
</MultiTrigger.Conditions>
<Setter TargetName="PART_Header"
Property="MinWidth"
Value="75" />
</MultiTrigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="HasHeader"
Value="false" />
<Condition Property="Height"
Value="Auto" />
</MultiTrigger.Conditions>
<Setter TargetName="PART_Header"
Property="MinHeight"
Value="19" />
</MultiTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
主要的事情是将expandCollapseTogleStyle
应用到您的切换按钮上,并确保您的ClickMode=“Press”
可以点击它。您可以在复制的控件模板中使用您应用的唯一ID对您的实例执行此临时操作,也可以根据您的喜好对其进行默认操作
希望这能有所帮助,欢迎来到这里。干杯:)如果您将按钮添加到标题模板中,则可以使用附加行为绑定按钮。嗨,Chris,它几乎完美无瑕,非常感谢您。问题是,它在奇怪的地方添加了很多多余的箭头。这是一个打印屏幕,我们希望第二个箭头正好位于正确对象的旁边。很抱歉,指向屏幕截图的链接似乎不起作用。在发布之前,我根本没有测试过这个解决方案,但对于您所指的行为,这肯定是一个简单的解决方案。