2

In WPF, I have a custom control that inherits from TreeView. The code is as follows...

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" }, }; } } 

The content in Generic.xaml looks as follows...

<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> 

I thought the Generic.xaml code should get applied to my control, and as such the ItemContainer property value should be set. But it looks like the ItemContainerStyle does not have any effect whatsoever.

NOTE: the HierarchicalDataTemplate from Generic.xaml DOES work ok, so the file is being interpreted.

Any ideas?

2
  • If you're doing MVVM you're mixing up your Models and ViewModels. Commented Nov 4, 2009 at 13:02
  • I "Think" I'm doing plain ViewModel, as per this CodeProject article - codeproject.com/KB/WPF/TreeViewWithViewModel.aspx So 'ViewModel' is maybe a confusing name for my data class. It should rather be something like 'MyDataObjectToDisplay'. Commented Nov 4, 2009 at 13:10

1 Answer 1

3

Questions of MVVM versus custom control aside, the problem is you've commented out the line that associates the style with your custom control:

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

Ergo, your control will just have the standard style for TreeViews.

Sign up to request clarification or add additional context in comments.

3 Comments

Does this matter? Since I'm just overriding the ItemContainerStyle, and not the Template?
Yes, as the ItemContainerStyle is contained within a Style that applies to CustomTRVs. The Style for CustomTRVs will never be applied because it hasn't been associated with the CustomTRV class. Try uncommenting the line.
Fantastic. That does the trick. I struggled a bit because I had to ensure that my Style is BasedOn the normal TreeView style. Your advice in addition to This did the trick: <Style TargetType="{x:Type local:CustomTRV}" BasedOn="{StaticResource {x:Type TreeView}}" >... Thanks Kent!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.