如何将全局样式应用于app.xaml中的wpf treeview
-
29-09-2019 - |
题
我找到了以下用于在焦点离开时在树视图中显示所选项目的代码,但是我很难将代码转移到app.xaml,因此任何usercontrol都可以使用它。
这做我想要的
<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:Static SystemColors.HighlightBrushKey}" Color="Peru"/>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black"/>
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Peru"/>
</TreeView.Resources>
但是我不知道如何从中制作出一种样式。我已经尝试了以下操作,句法是正确的
<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>
在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" >
在某一时刻,USERCORTROL代码认可了该样式,但目前显示出“资源Treestyle无法解决”。
我究竟做错了什么?
我需要范围范围,因为它在app.xaml中是一个不同的(父)名称空间?一旦使用该样式获得它,设置其他属性的语法是什么?
解决方案
该项目最初是作为类库设置的。我猜是在项目文件中的某个地方,它告诉WPF是否在app.xaml中查看,如果它的classLibrary将永远不会在那里看,除非您明确地告诉它在该文件中查看(我做了一个字典合并这似乎有效)。
当我记得它以前是一家库时,我弄清楚了这一点,所以我将代码复制到一个新项目,一切都很好。
不隶属于 StackOverflow