Kofoed

I have a control that inherits from TreeViewItem .... I basically want to find the control that houses (or contains) this TreeViewItem - i.e, the TreeView itself. I need to check a property on the TreeView and then modify an attribute in the TreeViewItem itself.

Is this possible



Re: Windows Presentation Foundation (WPF) How to find the parent of a TreeViewItem

Ben Carter - MSFT

You should be able to walk up the tree using ItemsControl.ItemsControlFromItemContainer(treeViewItem) until the return value is a TreeView.

Ben






Re: Windows Presentation Foundation (WPF) How to find the parent of a TreeViewItem

Kent Boogaart

Or if you're using your custom ListViewItem from XAML, you could just use a relative binding. Something like:

Code Block

<local:CustomTreeViewItem MyProperty="{Binding SomeProperty, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TreeView}}}"/>


HTH,
Kent




Re: Windows Presentation Foundation (WPF) How to find the parent of a TreeViewItem

Kofoed

Hmm ...

From code, I've tried the following two snippets:

DependencyObject dO = this.ItemContainerGenerator.ContainerFromItem(this);

DependencyObject dO2 = ItemsControl.ItemsControlFromItemContainer(this);

Where "this" is my custom TreeViewItem object ... both are returning null. I'm trying to check these from the constructor of the custom control ... something I'm missing somewhere





Re: Windows Presentation Foundation (WPF) How to find the parent of a TreeViewItem

Ben Carter - MSFT

this.ItemContainerGenerator.ContainerFromItem(this) will never work since "this" cannot be a child of itself. Also, if "this" is your custom TreeViewItem, then I would assume that you made it the container (i.e. you specified an override for IsItemItsOwnContainerOverride and GetContainerForItemOverride), meaning that you don't need to ask for the container.

Also, neither of the methods you used will ever work in the constructor of your custom control since at that point, nothing has been hooked up. You will have to wait until the container is part of the tree. You can use the Loaded event or some other event that works better for you.

Ben






Re: Windows Presentation Foundation (WPF) How to find the parent of a TreeViewItem

Kofoed

Interesting. I'm still somewhat of a noob when it comes to WPF ...

I haven't specified any container overrides for my custom TreeViewItem - at this point the only thing my custom class does is modify the style with a control template.

What I'm ultimately trying to do is locate the TreeView "parent" container for all my custom TreeViewItem objects ... and check a property in that treeview and then set a property in the TreeViewItem, depending on the value.

What's the best way to find the ultimate parent control (TreeView itself) from within my custom TreeViewItem





Re: Windows Presentation Foundation (WPF) How to find the parent of a TreeViewItem

Ben Carter - MSFT

For your scenario of setting a property on the TreeViewItem based on a property on the TreeView, you could use the binding that Kent provided. You may need to also specify a converter if you need to do a conversion, say from a bool to a color.

You could also define your own attached property that inherits. Then, on your TreeViewItem, you could specify a style in which there is a trigger that keys off your attached property where you set other properties.

I think these would solve your issue better than walking up the tree to the parent and manually setting the value, but that option is still open to you. In that case, I would handle the Loaded event on your custom TreeViewItem, perform the ItemsControlFromItemContainer call up the tree until you get to the TreeView, and then query and set your property. Unlike the first two options, this one will not automatically change if the source property changes (i.e. if the property on the TreeView changes, the first two options will update automatically, but this third option will require you to write more code to listen for changes to that property and update your custom TreeViewItem as appropriate).

Ben






Re: Windows Presentation Foundation (WPF) How to find the parent of a TreeViewItem

Kofoed

Thanks - I like the attached property scenario ... seems to fit the bill here. Are there any similar samples lying around you could point me to where attached properties are utilized





Re: Windows Presentation Foundation (WPF) How to find the parent of a TreeViewItem

Ben Carter - MSFT

Here are some snippets:

Code Block

private static readonly DependencyProperty TestProperty =

DependencyProperty.RegisterAttached("Test", typeof(int), typeof(Window1),

new FrameworkPropertyMetadata(-1, FrameworkPropertyMetadataOptions.Inherits));

public static int GetTest(DependencyObject obj)

{

return (int)obj.GetValue(TestProperty);

}

public static void SetTest(DependencyObject obj, int value)

{

obj.SetValue(TestProperty, value);

}

Code Block

<TreeView local:Window1.Test="1">

<TreeViewItem Header="Test">

<TreeViewItem.Style>

<Style TargetType="{x:Type TreeViewItem}">

<Style.Triggers>

<Trigger Property="local:Window1.Test" Value="1">

<Setter Property="Foreground" Value="Red" />

</Trigger>

</Style.Triggers>

</Style>

</TreeViewItem.Style>

</TreeViewItem>

</TreeView>

Ben