0

因此,我在使用自定义 treeviewitem:s 制作自定义树视图时遇到了这个问题,ItemContainerStyle通过从自定义样式加载样式来清除这些问题。

它像这样工作。我有基于 TreeViewItem 的自定义 MyTreeViewItem。

  <TreeViewItem x:Class="UI.MyTreeViewItem"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                 mc:Ignorable="d" 
                 d:DesignHeight="450" d:DesignWidth="800">
    
        <TreeViewItem.Resources>
    
            <Style x:Key="MyTreeViewItemStyle" TargetType="TreeViewItem">
                <Setter Property="Background" Value="#AEFFC1" />
            </Style>
    
        </TreeViewItem.Resources>
    
    </TreeViewItem>

正如你所看到的,我在这里只有一个简单的颜色,以确保它自己的样式有效。除非我在后面的代码中这样做,否则这永远不会加载。

编辑:我知道不需要将诸如着色之类的东西放在这里,因为这里本来打算有一个模板。自从注意到真正起作用以来,我只是把它剥离到骨头上,以确保我放了一些超级简单的东西,我知道它应该可以工作,以防万一它是因为它本身的模板。

    public partial class MyTreeViewItem : TreeViewItem
    {
        public MyTreeViewItem()
        {
            InitializeComponent();
            this.Loaded += MyTreeViewItem_Loaded;          
        }

        private void MyTreeViewItem_Loaded(object sender, RoutedEventArgs e)
        {
           this.Style = Resources["MyTreeViewItemStyle"] as Style;
        }
   }

这很有效。已经将它与其他控件一起使用过几次,以便为需要加载的控件自定义样式,而不必费心一遍又一遍地“重新设计”所有内容。

我怎么会遇到这个问题。这就是ItemContainerStyle这种自定义样式控制器的使用时间。

<local:BaseTreeView x:Class="My.Navigator.NavigatorTreeView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:ui="clr-namespace:UI;assembly=BaseCode" 
             d:DesignHeight="450" d:DesignWidth="800">

<ui:MyTreeView ItemsSource="{Binding Path=Nodes}">   
...

        <ui:MyTreeView.ItemContainerStyle>
            <Style TargetType="{x:Type ui:MyTreeViewItem}">
                <Setter Property="FontWeight" Value="Normal" />
                <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}"/>
                <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />

                <EventSetter Event="Selected" Handler="TreeView_SelectedItemChanged" />
                <EventSetter Event="Expanded" Handler="TreeView_NodeExpanded" />
                <EventSetter Event="Collapsed" Handler="TreeView_NodeCollapsed" />

                <Style.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="FontWeight" Value="Bold" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </ui:MyTreeView.ItemContainerStyle>
    </ui:MyTreeView>
</local:BaseTreeView>

正如你在 ui:MyTreeView.ItemContainerStyle上面看到的,一旦样式被加载,就会被完全忽略,this.Style = Resources["MyTreeViewItemStyle"] as Style;MyTreeViewItem_Loaded. 这意味着这些 Setter、EventSetter 和 Triggers 根本不会触发,因为它们仍然需要能够作为附加规则添加。

如何解决这个问题,以便可以加载自定义控件中的预定义样式,并且通过使用此控件,您仍然可以像上面那样连接独特的规则,而无需预定义的否决它们?

4

1 回答 1

1

目前还不清楚你为什么要做你正在做的事情。我只能说,Style在控件使用来自TreeView.ItemContainerStyle.

通常,在 a 上UserControl,您会在元素上本地设置属性:

<TreeViewItem x:Class="UI.MyTreeViewItem" 
              ...
              d:DesignHeight="450" d:DesignWidth="800"
              Background="#AEFFC1">
</TreeViewItem>

或在代码隐藏中:

private void MyTreeViewItem_Loaded(object sender, RoutedEventArgs e)  
{
  this.Background = 
    new SolidColorBrush(ColorConverter.ConvertFromString("#AEFFC1"));
 }

编写 custom时,您将在 Generic.xaml 中Control提供默认值。Style这是最好的解决方案,因为它允许为控件设置样式(允许自定义样式覆盖 default 提供的默认值Style)。外部样式被隐式合并。您应该更喜欢自定义控件而不是 UserControl。

您当前的代码不允许样式化,因为您强制覆盖自定义提供的值Style

// Overwrite previous property value.
this.Style = someValue;

这是编程 101,一年级:赋值总是覆盖变量的旧值(引用)。

假设您知道自己在做什么并且不想使用上述解决方案之一,则必须使用 Style.BasedOn 属性手动合并两种样式:

private void MyTreeViewItem_Loaded(object sender, RoutedEventArgs e)
{
  var defaultStyle = Resources["MyTreeViewItemStyle"] as Style;
  defaultStyle.BasedOn = this.ItemContainerStyle;
  this.Style = defaultStyle;
}

请参阅:控件创作概述:控件创作模型

于 2021-10-14T09:46:40.433 回答