過去幾年我一直在閱讀這裏的問題和答案,但這是我無法準確找到所需內容後的第一個問題。WPF TreeView的多個展開按鈕
我正在創建一個WPF MVVM應用程序。 在這個應用程序中,我使用了一個TreeView,其中的每個節點都是兩個對象的組合(讓我們假設字符串)。
我想補充另一個展開按鈕,從節點左側的原始的一個,和另一個從節點的右側。
我想添加一個按鈕到TreeViewItem標題,但我不明白(搜索後)如何將他綁定到擴展操作。
很想聽到一些想法......
過去幾年我一直在閱讀這裏的問題和答案,但這是我無法準確找到所需內容後的第一個問題。WPF TreeView的多個展開按鈕
我正在創建一個WPF MVVM應用程序。 在這個應用程序中,我使用了一個TreeView,其中的每個節點都是兩個對象的組合(讓我們假設字符串)。
我想補充另一個展開按鈕,從節點左側的原始的一個,和另一個從節點的右側。
我想添加一個按鈕到TreeViewItem標題,但我不明白(搜索後)如何將他綁定到擴展操作。
很想聽到一些想法......
所以,如果我們去看看在default template爲x:Key="{x:Type TreeViewItem}"
我們有一個ToggleButton
這樣看;
<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>
主要的是剛剛申請的是ExpandCollapseToggleStyle
您切換按鈕,並確保您的ClickMode="Press"
這樣你就可以打它。您可以使用您應用的唯一ID複製的控制模板或您的默認值(無論您喜歡什麼)爲您的實例創建特定目錄。
希望這有助於,並歡迎來到SO。歡呼聲:)
嗨,克里斯 它的工作幾乎完美,所以非常感謝你。 問題是它在奇怪的地方增加了很多冗餘箭頭這裏是一個打印屏幕[鏈接](https://fbcdn-sphotos-ha.akamaihd.net/hphotos-ak-xpf1/v/t34.0-12/ 10421426_10204038279343695_3421096564790375729_n.jpg?哦= 9183ebccc95fcc33ab0c339455b966ed&OE = 53983DD8&__ GDA __ = 1402457079_8a508c356a623d47e2bf9d78fb8288d) ,我們希望第二個箭頭只是一個如何旁邊就是正確的對象.. – slashms
對不起鏈接到您的截圖似乎並不奏效。在發佈之前,我根本沒有測試過這個解決方案,但對於您所指的行爲來說,這肯定是一個簡單的解決方案。 –
如果將按鈕添加到標題模板,則可以使用附加的行爲綁定按鈕。 –