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.
精彩评论