1

我找到了以下代码,用于在焦点离开时在树视图中显示所选项目,但我无法将代码移动到 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 中?一旦我使用样式得到它,设置其他属性的语法是什么?

4

1 回答 1

0

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

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

于 2011-04-29T18:24:31.437 回答