如何将全局样式应用于 App.xaml 中的 wpf 树视图

发布于 2024-09-27 08:10:09 字数 1799 浏览 1 评论 0原文

我找到了以下代码,用于在焦点离开时在树视图中显示所选项目,但我无法将代码移至 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" >

一度用户控件代码识别了样式,但目前它显示“资源 TreeStyle 无法解析”。
我做错了什么?
我是否需要确定 TreeStyle 的范围,因为它位于 App.xaml 中,这是一个不同的(父)命名空间? 一旦我使用样式获得它,设置其他属性的语法是什么?

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: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>

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绻影浮沉 2024-10-04 08:10:09

该项目最初被设置为类库。我猜测,在项目文件中的某个位置,它告诉 WPF 是否要在 App.xaml 中查找,如果它是一个 ClassLibrary,它永远不会在那里查找,除非您专门告诉它在该文件中查找(我做了一个字典合并这似乎有效)。

当我记得它以前是一个类库时,我想到了这一点,所以我将代码复制到一个新项目中,一切都很好。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文