5

I have created a simple dependency property that I want to attach to a treeViewitem, I have done similar things for other controls such as buttons but cannot figure out how to use the TreeViewItem in a treeview without loosing my defined style. With the code below I get "A style intended for type 'ErrorTreeViewItem' cannot be applied to type 'TreeViewItem'."

public class ErrorTreeViewItem : TreeViewItem
{
    static ErrorTreeViewItem()
    {
    }

    public bool ErrorState
    {
        get { return (bool)GetValue(ErrorStateProperty); }
        set { base.SetValue(ErrorStateProperty, value); }
    }

    public static readonly DependencyProperty ErrorStateProperty =
        DependencyProperty.Register("ErrorState", typeof(bool), typeof(ErrorTreeViewItem), new UIPropertyMetadata(false));
}

The style of my tree view looks like:

      <Style TargetType="me:ErrorTreeViewItem">

        <Style.Resources>
           ...
        </Style.Resources>
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
            <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="TreeViewItem">
                ...
            </Setter.Value>
        </Setter>

I am using it like:

    <TreeView Name="ApplicationTree" ItemsSource="{Binding Applications}" HorizontalContentAlignment="Stretch" Background="#E8E8E8" >
        <TreeView.ItemContainerStyle>
            <Style TargetType="me:ErrorTreeViewItem" BasedOn="{StaticResource {x:Type TreeViewItem}}">
                <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
                <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
                <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
            </Style>
        </TreeView.ItemContainerStyle>
3
  • Are you not using the built in validation services because you don't understand them (they're tough to get right!) or because you didn't know they exist? IDataErrorInfo.
    – user1228
    Feb 27, 2012 at 18:31
  • No its not that I just want to change the colour of a treeview item based on the value of my dependency property. Didn't think it would be this hard! Feb 27, 2012 at 18:34
  • 1
    Well, there are a number of ways to do this. IDataErrorInfo on your Models (if you are using MVVM) will automatically trigger validation and show the invalid template on the standard WPF controls. You could also try using an attached DependencyProperty on the TreeViewItem, so you don't even need to extend the base class, etc. I've never done it this way in WPF, but I know to accomplish this in Winforms you have to extend the TreeView class as well, so when you create a new TVI you return an instance of your type instead. That's where your error is cropping up.
    – user1228
    Feb 27, 2012 at 18:39

2 Answers 2

2

You can override GetContainerForItemOverride on your TreeView (Yes, you need to subclass TreeView too).

protected override DependencyObject GetContainerForItemOverride()
{
    return new YourCustomTreeViewItem();
}

Note that you need to override GetContainerForItem on your custom TreeViewItem too, if you want them to generate subitems of that type.

1

The TreeView will create default TreeViewItems so you first need to make it create your tree view items. To do so you will need to subclass TreeView and override PrepareContainerForItem to return a new instance of ErrorTreeViewItem.

1
  • PrepareContainerForItem doesn't exist and PrepareContainerForItemOverride doesn't return anything.
    – fractor
    Nov 20, 2023 at 7:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.