我找到了以下代码,用于在焦点离开时在树视图中显示所选项目,但我无法将代码移动到 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>
并在用户控件中
<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" >
在某一时刻,UserControl 代码识别出该样式,但目前它显示“资源 TreeStyle 无法解析”。
我究竟做错了什么?
我是否需要确定 TreeStyle 的范围,因为它位于不同(父)命名空间的 App.xaml 中?一旦我使用样式得到它,设置其他属性的语法是什么?