【问题标题】:In WPF, why does my Style in Generic.xaml not get applied to my Custom Control?在 WPF 中,为什么我在 Generic.xaml 中的样式没有应用于我的自定义控件?
【发布时间】:2010-12-13 00:22:33
【问题描述】:

在 WPF 中,我有一个继承自 TreeView 的自定义控件。代码如下...

public class CustomTRV : TreeView
{
    static CustomTRV()
    {
        //Removed this because I want the default TreeView look.
        //......CustomTRV.DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomTRV), new FrameworkPropertyMetadata(typeof(CustomTRV)));
    }

    public void Connect(string entityHierarchyToken)
    {
        //build viewModel classes... 
        this.ItemsSource = new List<ViewModel>()
        {
            new ViewModel() { TextValue = "aaaa" },
            new ViewModel() { TextValue = "bbb" },
            new ViewModel() { TextValue = "ccc" },
            new ViewModel() { TextValue = "ddd" },
            new ViewModel() { TextValue = "eee" },
        };
    }
}

Generic.xaml 中的内容如下...

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfTestCustomControl">

    <HierarchicalDataTemplate DataType="{x:Type local:ViewModel}">
        <TextBlock Foreground="Blue" Text="{Binding Path=TextValue}"></TextBlock>
    </HierarchicalDataTemplate>

    <Style TargetType="{x:Type local:CustomTRV}">
        <Setter Property="ItemContainerStyle">
            <Setter.Value>

                <Style TargetType="{x:Type TreeViewItem}">
                    <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
                    <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
                    <Setter Property="FontWeight" Value="Bold" />
                    <Style.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="FontWeight" Value="Normal" />
                        </Trigger>
                    </Style.Triggers>
                </Style>

            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

我认为应该将 Generic.xaml 代码应用于我的控件,因此应该设置 ItemContainer 属性值。但看起来 ItemContainerStyle 没有任何效果。

注意:Generic.xaml 中的 HierarchicalDataTemplate 工作正常,因此正在解释文件。

有什么想法吗?

【问题讨论】:

  • 如果你在做 MVVM,你就是在混淆你的模型和视图模型。
  • 我“认为”我正在做普通的 ViewModel,根据这篇 CodeProject 文章 - codeproject.com/KB/WPF/TreeViewWithViewModel.aspx 所以“ViewModel”对于我的数据类来说可能是一个令人困惑的名称。它应该类似于“MyDataObjectToDisplay”。

标签: wpf wpf-controls generic.xaml


【解决方案1】:

除了 MVVM 与自定义控件的问题,问题在于您已经注释掉了将样式与自定义控件相关联的行:

//CustomTRV.DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomTRV), new FrameworkPropertyMetadata(typeof(CustomTRV)));

所以,您的控件将只有 TreeViews 的标准样式。

【讨论】:

  • 这有关系吗?因为我只是覆盖了 ItemContainerStyle,而不是模板?
  • 是的,因为 ItemContainerStyle 包含在适用于 CustomTRV 的 Style 中。永远不会应用 CustomTRV 的样式,因为它尚未与 CustomTRV 类关联。尝试取消注释该行。
  • 太棒了。这就是诀窍。我有点挣扎,因为我必须确保我的样式是基于正常的 TreeView 样式。除了这之外,您的建议成功了:
猜你喜欢
  • 1970-01-01
  • 2023-04-02
  • 1970-01-01
  • 1970-01-01
  • 2016-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多