如何合并自定义的树视图样式和皮肤



我想使我的自定义treeViewItem样式剥皮。我试图遵循有关此事的教程,但是从简单的情况到多个文件中的多个资源词典中,我都在抽象中遇到问题。

我在文件中为我的treeviewitem定义了一种自定义样式:

<ResourceDictionary  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="*****namespace omitted******">
    <Style x:Key="GroupedTreeViewItemStyle" TargetType="{x:Type TreeViewItem}">
        <!-- Most of the content omitted, see below as an example of skin reference -->
        <ControlTemplate TargetType="{x:Type TreeViewItem}">
            <ControlTemplate.Triggers>
                <Trigger Property="IsSelected" Value="true">
                    <Setter Property="Background" TargetName="Bd" Value="{DynamicResource BackgroundHighlightBrush}"/>
                    <Setter Property="Foreground" Value="{DynamicResource TextHighlightBrush}"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Style>
</ResourceDictionary>

然后我们有默认的皮肤,定义了我们需要的两个刷子:

<ResourceDictionary  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="*****namespace omitted*****">
    <!-- Text Color brushes -->
    <SolidColorBrush x:Key="TextHighlightBrush" Color="White"/>

    <!-- Box Color Brushes -->
    <SolidColorBrush x:Key="BackgroundHighlightBrush" Color="Black"/>
</ResourceDictionary>

最后,我尝试将两个资源词典添加到树视图项目资源如下:

<TreeView Name="treeView" ItemsSource="{Binding}" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
    <TreeView.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary>
                    <Style TargetType="TreeViewItem">
                        <Setter Property="IsExpanded"  Value="{Binding Expanded, Mode=TwoWay}"/>
                    </Style>
                </ResourceDictionary>
                <ResourceDictionary  Source="../../Skins/Default.xaml"/>
                <ResourceDictionary  Source="GroupedTreeViewItemStyle.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </TreeView.Resources>
</TreeView>

但没有任何资源词典应用。你知道为什么吗?

预先感谢您的帮助!

GroupedTreeViewItemStyle存在但在任何地方都不应用。您可以使用BasedOn属性从该样式得出默认的TreeViewItem样式:

<TreeView.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary  Source="../../Skins/Default.xaml"/>
            <ResourceDictionary  Source="GroupedTreeViewItemStyle.xaml"/>
        </ResourceDictionary.MergedDictionaries>
        <Style TargetType="TreeViewItem" BasedOn="{StaticResource GroupedTreeViewItemStyle}">
            <Setter Property="IsExpanded"  Value="{Binding Expanded, Mode=TwoWay}"/>
        </Style>
    </ResourceDictionary>
</TreeView.Resources>

最新更新