【发布时间】: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