您的位置:首页 > 其它

WPF新手之如何自定义TreeView点击后的背景色

2010-09-16 17:14 459 查看
其它控件也同样适用:

对于一时找不出好办法的情况,直接用StyleSnooper找到所需的控件,查看它的默认Style。然后找到所需的设置,如这里是找到

<Trigger Property="TreeViewItem.IsSelected">
                        <Setter Property="Panel.Background" TargetName="Bd">
                            <Setter.Value>
                                <DynamicResource ResourceKey="{x:Static SystemColors.HighlightBrushKey}" 
/>
                                </Setter.Value>
                            </Setter>
                        <Setter Property="TextElement.Foreground">
                            <Setter.Value>
                                <DynamicResource ResourceKey="{x:Static 
SystemColors.HighlightTextBrushKey}" />
                                </Setter.Value>
                            </Setter>
                        <Trigger.Value>
                            <s:Boolean>
                                True</s:Boolean>
                            </Trigger.Value>
                        </Trigger>
<MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="TreeViewItem.IsSelected">
                                        <Condition.Value>
                                            <s:Boolean>
                                                True</s:Boolean>
                                        </Condition.Value>
                                    </Condition>
                                    <Condition Property="Selector.IsSelectionActive">
                                        <Condition.Value>
                                            <s:Boolean>
                                                False</s:Boolean>
                                        </Condition.Value>
                                    </Condition>
                                </MultiTrigger.Conditions>
                                <Setter Property="Panel.Background" TargetName="Bd">
                                    <Setter.Value>
                                        <DynamicResource ResourceKey="{x:Static 
SystemColors.ControlBrushKey}" />
                                    </Setter.Value>
                                </Setter>
                                <Setter Property="TextElement.Foreground">
                                    <Setter.Value>
                                        <DynamicResource ResourceKey="{x:Static 
SystemColors.ControlTextBrushKey}" />
                                    </Setter.Value>
                                </Setter>
                            </MultiTrigger>





这是当项被选中之时的触发器。现在只要把对应的值{x:Static SystemColors.HighlightBrushKey}在Style.Resources中重新定义即可:

<Style.Resources>
                <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="PapayaWhip"/>
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="PapayaWhip"/>
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black"/>
            </Style.Resources>



两个注意点:

①原来Style中可能会有自定义的Resources,也许是出于完备性的考虑,其实并没有内容

<Style.Resources>
                    <ResourceDictionary />
                    </Style.Resources>



这个需要把它删除,因为只能出现一处。

②可能需要在Windows中加入命名空间

xmlns:s="clr-namespace:System;assembly=mscorlib"



但是尚有两个问题未决:

①由于默认Style中很多地方相互引用,因此往往需要把整个Style全部拷贝过来(也许我水平高了以后可以不这样)

②由于①的原因,TreeViewItem前面的小三角失去了效果。



PS:学后记:其实根本不用这么麻烦,只要找到默认Style中的值,在Resources中将相应的值进行重新定义即可:

<Grid.Resources>
            <Style TargetType="TreeViewItem">
                <Style.Resources>
                    <!--SelectedItem with focus-->
                    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="LightBlue" Opacity=".2"/>
                    <!--SelectedItem without focus-->
                    <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="LightBlue" Opacity=".2"/>
                </Style.Resources>
            </Style>
        </Grid.Resources>


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: