Typed Style和设置ItemContainerStyle之间的区别

时间:2016-08-02 12:36:35

标签: c# wpf treeview treeviewitem

Style的{​​{1}}目前适用于所有人,因为他没有TreeViewItems

Key

当我离开它时,一切都像我想要的那样。但是,如果我给<Style TargetType="TreeViewItem"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="TreeViewItem"> <Grid x:Name="gChildren"> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <Rectangle x:Name="HorLin" Grid.Row="0" Height="1" Stroke="Black" SnapsToDevicePixels="True" VerticalAlignment="Bottom" /> <Rectangle x:Name="VerLinUp" Grid.Row="1" Width="1" Height="20" Stroke="Black" SnapsToDevicePixels="True" /> <Border Name="Bd" Background="{TemplateBinding Background}" BorderBrush="Black" BorderThickness="3" Padding="6" Grid.Row="2" Margin="2,0"> <ContentPresenter Name="PART_Header" ContentSource="Header" HorizontalAlignment="Center" VerticalAlignment="Center" /> </Border> <Rectangle x:Name="VerLinDown" Grid.Row="3" Width="1" Height="10" Stroke="Black" SnapsToDevicePixels="True" /> <ItemsPresenter x:Name="itemPresenter" Grid.Row="4" HorizontalAlignment="Center"/> </Grid> <ControlTemplate.Triggers> <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Items.Count, Converter={StaticResource IsGreaterThanConv}, ConverterParameter=0}" Value="false"> <Setter TargetName="VerLinDown" Property="Visibility" Value="Collapsed" /> </DataTrigger> <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource HasParentMoreChildren}}" Value="false"> <Setter TargetName="HorLin" Property="Visibility" Value="Collapsed" /> </DataTrigger> <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource IsParentTreeViewItem}}" Value="false"> <Setter TargetName="VerLinUp" Property="Visibility" Value="Collapsed" /> </DataTrigger> <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource IsParentTreeViewItem}}" Value="true"> <Setter TargetName="VerLinUp" Property="Height" Value="10" /> </DataTrigger> <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource IsFirstOrLastItem}}" Value="2"> <Setter TargetName="HorLin" Property="Width" Value="{Binding ElementName=gChildren, Path=ActualWidth, Converter={StaticResource ArithmeticConverter}, ConverterParameter=/2}" /> <Setter TargetName="HorLin" Property="HorizontalAlignment" Value="Right" /> </DataTrigger> <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource IsFirstOrLastItem}}" Value="1"> <Setter TargetName="HorLin" Property="Width" Value="{Binding ElementName=gChildren, Path=ActualWidth, Converter={StaticResource ArithmeticConverter}, ConverterParameter=/2}" /> <Setter TargetName="HorLin" Property="HorizontalAlignment" Value="Left" /> </DataTrigger> <Trigger Property="IsSelected" Value="True"> <Setter TargetName="Bd" Property="Panel.Background" Value="{StaticResource SelectedItemAreaBrush}" /> <Setter TargetName="Bd" Property="Border.BorderBrush" Value="{StaticResource SelectedItemBorderBrush}" /> <Setter TargetName="Bd" Property="TextElement.Foreground" Value="White" /> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> <Setter Property="ItemsPanel"> <Setter.Value> <ItemsPanelTemplate> <StackPanel x:Name="spChildren" HorizontalAlignment="Center" IsItemsHost="True" Margin="4,0,4,6" Orientation="Horizontal" /> </ItemsPanelTemplate> </Setter.Value> </Setter> </Style> 一个姓名,并在Style上设置ItemContainerStyle就会有所不同

TreeView

<Style x:Key="GoodTVI" TargetType="TreeViewItem"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="TreeViewItem"> <Grid x:Name="gChildren"> - 样式添加到我的TreeViewItem

TreeView

TVVerlauf.SetBinding(TreeView.ItemContainerStyleProperty, "GoodTVI"); 的其余部分保持完全相同。这是唯一的区别,但结果看起来完全不同。

2 个答案:

答案 0 :(得分:0)

  

我给我的样式一个名字

实际上,你没有。

<Style x:Key="GoodTVI" TargetType="TreeViewItem">
    ...

那不是名字。这是关键。 Style是一种资源。资源有密钥,而不是名称。两者是不同的。首先,资源键是对象而不是字符串。他们在不同的地方以不同的方式抬头。

在这里,您将树视图的ItemContainerStyle属性绑定到其GoodTVI的{​​{1}}属性(应该是您的viewmodel)。

DataContext

由于TVVerlauf.SetBinding(TreeView.ItemContainerStyleProperty, "GoodTVI"); 是资源键,而不是viewmodel上属性的名称,因此您自然无法获得良好的结果。

在XAML中的GoodTVI上尝试此操作:

TreeView

如果您需要在C#中执行此操作,请尝试以下操作:

<TreeView
    ItemContainerStyle="{StaticResource GoodTVI}"
    ...other properties...
    >

答案 1 :(得分:-1)

试试这个:

TVVerlauf.SetBinding(TreeView.StyleProperty, "GoodTVI");