在 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 工作正常,因此正在解释文件。
有任何想法吗?