[英]How to apply a global style to a wpf treeview in App.xaml
我發現以下代碼在焦點離開時在樹狀視圖中顯示所選項目,但是我無法將代碼移至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中是一個不同的(父)命名空間? 使用樣式獲取它后,設置其他屬性的語法是什么?
該項目最初設置為類庫。 我猜在項目文件中的某個地方,它會告訴WPF是否在App.xaml中查找,如果它是ClassLibrary,它將永遠不會出現在其中,除非您明確告訴它要在該文件中查找(我做了字典合並)似乎有效)。
當我想起它以前是一個類庫時,我發現了這一點,所以我將代碼復制到了一個新項目中,一切都很好。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.