gpt4 book ai didi

c# - 子项目的 TreeView ItemContainerStyle,FullRowSelect

转载 作者:行者123 更新时间:2023-11-30 22:21:02 26 4
gpt4 key购买 nike

已经使用方法为TreeViewItems设置了一个fullrowselect这里:Lee Campbell horizontal stretch for treeviewitems

这适用于顶级项目。

但是 TreeViewItem.Items 似乎有问题集合,这似乎没有采用正确的模板。(我看到它不在 TreeView.Items 集合中,正如模板所建议的那样)

有没有一种简单的方法可以从父项样式派生子项样式?

如何一次描述样式(对于所有 TreeviewItems)并让样式用于所有 TreeViewItems,无论它们是在 TreeView.Items 还是在 TreeViewItem.Items 集合中?

这是我的 XAML,一些代码来自 LeeCampbell 示例。

<Window x:Class="TreeViewFullRow.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TreeViewFullRow"
Title="MainWindow"
Width="525"
Height="350">

<Window.Resources />

<Grid>

<TreeView Name="TreeData"
Width="236"
Height="311"
HorizontalAlignment="Left"
VerticalAlignment="Top">

<TreeView.ItemContainerStyle>




<Style BasedOn="{StaticResource {x:Type TreeViewItem}}" TargetType="TreeViewItem">
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TreeViewItem">
<StackPanel>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" MinWidth="19" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<!--
Note that the following do not work, but I believe the top 2 should?!
<ToggleButton IsChecked="{TemplateBinding IsExpanded}" ClickMode="Press" Name="Expander">
<ToggleButton IsChecked="{TemplateBinding Property=IsExpanded}" ClickMode="Press" Name="Expander">
<ToggleButton IsChecked="{TemplateBinding Path=IsExpanded}" ClickMode="Press" Name="Expander">
-->
<ToggleButton Name="Expander"
ClickMode="Press"
IsChecked="{Binding Path=IsExpanded,
RelativeSource={RelativeSource TemplatedParent}}">
<ToggleButton.Style>
<Style TargetType="ToggleButton">
<Setter Property="UIElement.Focusable" Value="false" />
<Setter Property="FrameworkElement.Width" Value="16" />
<Setter Property="FrameworkElement.Height" Value="16" />
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate TargetType="ToggleButton">
<Border Width="16"
Height="16"
Background="#00FFFFFF"
Padding="5,5,5,5">
<Path Name="ExpandPath"
Fill="#00FFFFFF"
Stroke="#FF989898">
<Path.Data>
<PathGeometry Figures="M0,0L0,6L6,0z" />
</Path.Data>
<Path.RenderTransform>
<RotateTransform Angle="135" CenterX="3" CenterY="3" />
</Path.RenderTransform>
</Path>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="UIElement.IsMouseOver" Value="True">
<Setter TargetName="ExpandPath" Property="Shape.Stroke" Value="#FF1BBBFA" />
<Setter TargetName="ExpandPath" Property="Shape.Fill" Value="#00FFFFFF" />
</Trigger>
<Trigger Property="ToggleButton.IsChecked" Value="True">
<Setter TargetName="ExpandPath" Property="UIElement.RenderTransform">
<Setter.Value>
<RotateTransform Angle="180" CenterX="3" CenterY="3" />
</Setter.Value>
</Setter>
<Setter TargetName="ExpandPath" Property="Shape.Fill" Value="#FF595959" />
<Setter TargetName="ExpandPath" Property="Shape.Stroke" Value="#FF262626" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ToggleButton.Style>
</ToggleButton>
<Border x:Name="Bd"
Grid.Column="1"
HorizontalAlignment="Stretch"
Background="{TemplateBinding Panel.Background}"
BorderBrush="{TemplateBinding Border.BorderBrush}"
BorderThickness="{TemplateBinding Border.BorderThickness}"
Padding="{TemplateBinding Control.Padding}"
SnapsToDevicePixels="True">
<ContentPresenter x:Name="PART_Header"
HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}"
Content="{TemplateBinding HeaderedContentControl.Header}"
ContentSource="Header"
ContentStringFormat="{TemplateBinding HeaderedItemsControl.HeaderStringFormat}"
ContentTemplate="{TemplateBinding HeaderedContentControl.HeaderTemplate}"
ContentTemplateSelector="{TemplateBinding HeaderedItemsControl.HeaderTemplateSelector}"
SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
</Border>
<ItemsPresenter x:Name="ItemsHost"
Grid.Row="1"
Grid.Column="1" />
</Grid>
</StackPanel>
<ControlTemplate.Triggers>
<Trigger Property="TreeViewItem.IsExpanded" Value="False">
<Setter TargetName="ItemsHost" Property="UIElement.Visibility" Value="Collapsed" />
</Trigger>
<Trigger Property="ItemsControl.HasItems" Value="False">
<Setter TargetName="Expander" Property="UIElement.Visibility" Value="Hidden" />
</Trigger>
<Trigger Property="TreeViewItem.IsSelected" Value="True">
<Setter TargetName="Bd" Property="Panel.Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
<Setter Property="TextElement.Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}" />
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="TreeViewItem.IsSelected" Value="True" />
<Condition Property="Selector.IsSelectionActive" Value="False" />
</MultiTrigger.Conditions>
<Setter TargetName="Bd" Property="Panel.Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" />
<Setter Property="TextElement.Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" />
</MultiTrigger>
<Trigger Property="UIElement.IsEnabled" Value="False">
<Setter Property="TextElement.Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>




</TreeView.ItemContainerStyle>

</TreeView>


<Button Name="BtnLoad"
Width="75"
Height="23"
Margin="277,12,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Click="OnBtnLoad"
Content="Load data" />
</Grid>
</Window>

这很容易。在按钮单击处理程序中,我将新项目添加到 TreeView 。这是代码:

private void OnBtnLoad(object sender, RoutedEventArgs e)
{
var count = this.TreeData.Items.Count.ToString();
var parent = new TreeViewItem() { Name = "Name_" + count, Header = "Header_" + count };

this.TreeData.Items.Add(parent);

var child0 = new TreeViewItem() { Name = "Name_" + count + "_0", Header = "Header_" + count + "_0" };
var child1 = new TreeViewItem() { Name = "Name_" + count + "_1", Header = "Header_" + count + "_1" };
var child2 = new TreeViewItem() { Name = "Name_" + count + "_2", Header = "Header_" + count + "_2" };

parent.Items.Add(child0);
parent.Items.Add(child1);
parent.Items.Add(child2);

parent.Items.Refresh();
}

最佳答案

如果您将样式放在可视化树的更上方,它会影响 TreeView 中的项目和子项目。例如,您可以将样式放在 Grid.ResourcesWindow.Resources 中,而不是像现在那样将其放在 TreeView.ItemContainerStyle 中.

关于c# - 子项目的 TreeView ItemContainerStyle,FullRowSelect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14624929/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com