开发者

How to apply a global style to a wpf treeview in App.xaml

开发者 https://www.devze.com 2023-01-20 10:31 出处:网络
I found the following code for showing the selected item in a treeview when focus has left, but I\'m having trouble moving the code to App.xaml so any UserControl could use it.

I found the following code for showing the selected item in a treeview when focus has left, but I'm having trouble moving the code to App.xaml so any UserControl could use it.

This does what I want

<TreeView x:Name="trviewArchives" Width="141" Height="154" Canvas.Left="20" Canvas.Top="167"  Background="{x:Null}" BorderBrush="#FF081827" BorderThickness="0">
            <TreeView.Resources>
                <TreeViewItem x:Key="bold" FontWeight="Bold" />
                <SolidColorBrush x:Key="{x:S开发者_如何转开发tatic SystemColors.HighlightBrushKey}" Color="Peru"/>
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black"/>
                <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Peru"/>
            </TreeView.Resources>

But I can't figure out how to make a style out of it. I've tried the following, which seams syntactically correct

            <Style x:Key="TreeStyle" TargetType="{x:Type TreeView}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="TreeViewItem">
                        <TreeViewItem>
                            <Setter x:Name="bold" Property="FontWeight" Value="Bold" />
                        </TreeViewItem>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

And in the usercontrol

<TreeView x:Name="trviewArchives" Width="141" Height="154" Canvas.Left="20" Canvas.Top="167"  Background="{x:Null}"  Style="{DynamicResource ResourceKey=TreeStyle}"
                  BorderBrush="#FF081827" BorderThickness="0" >

At one point the UserControl code recognized the style, but currently its showing "resource TreeStyle could not be resolved".

What am i doing wrong?

Do I need to scope TreeStyle since its in App.xaml which is a different (parent) namespace? Once I get it using the style, what is the syntax for setting the other properties?


The project was initially setup as a Class Library. Somewhere in the project file, I'm guessing, it tells WPF whether to look in the App.xaml, and if its a ClassLibrary it will never look there, unless you specifically tell it to look in that file (I did a dictionary merge that seems to work).

I figured this out when i remembered that it was previously a Class Library, so i copied the code to a new project and all was good.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号