【问题标题】:Extend Custom TreeView itemcontainers style?扩展自定义 TreeView 项目容器样式?
【发布时间】:2013-02-25 22:00:29
【问题描述】:

如何扩展我在 CustomTreeView 中声明的样式?这样我就有了浅灰色的前景和绿色的背景?

CustomTreeView.xaml

<TreeView x:Class="WpfApplication17.CustomTreeView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <TreeView.ItemContainerStyle>
        <Style TargetType="{x:Type TreeViewItem}">
            <Setter Property="Foreground" Value="LightGray"/>
        </Style>
    </TreeView.ItemContainerStyle>
</TreeView>

Window.xaml

<Window x:Class="WpfApplication17.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication17">
    <local:CustomTreeView ItemsSource="{Binding Data}">
        <local:CustomTreeView.ItemContainerStyle>
            <Style TargetType="{x:Type TreeViewItem}" BasedOn="{StaticResource {x:Type TreeViewItem}}">
                <Setter Property="Background" Value="Green"/>
            </Style>
        </local:CustomTreeView.ItemContainerStyle>
    </local:CustomTreeView>
</Window>

【问题讨论】:

    标签: wpf styles itemcontainerstyle basedon


    【解决方案1】:

    不确定这是否是正确的方法,但我最终使用了它。

    <TreeView x:Class="WpfApplication17.CustomTreeView"
                     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
            <TreeView.Resources>
                <Style x:Key="m_itemContainerStyle" TargetType="{x:Type TreeViewItem}">
                    <Setter Property="Foreground" Value="LightGray"/>
                </Style>
            </TreeView.Resources>
    
            <TreeView.ItemContainerStyle>
                <Style BasedOn="{StaticResource m_itemContainerStyle}" TargetType="{x:Type TreeViewItem}">
    
                </Style>
            </TreeView.ItemContainerStyle>
        </TreeView>
    
    
        <Window x:Class="WpfApplication17.MainWindow"
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:local="clr-namespace:WpfApplication17">
            <local:CustomTreeView ItemsSource="{Binding Data}">
                <local:CustomTreeView.ItemContainerStyle>
                    <Style BasedOn="{StaticResource m_itemContainerStyle}"  TargetType="{x:Type TreeViewItem}">
                        <Setter Property="Background" Value="Green"/>
                    </Style>
                </local:CustomTreeView.ItemContainerStyle>
            </local:CustomTreeView>
        </Window>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-09-13
      • 1970-01-01
      • 2013-07-06
      • 1970-01-01
      • 2012-07-18
      • 2016-08-14
      • 2011-10-04
      相关资源
      最近更新 更多