Rei Miyasaka

This pukes up an exception:

Code Snippet

<TreeView>
<TreeView.Resources>
<ContextMenu x:Key="TestMenu">
<MenuItem Header="Item 1"/>
<MenuItem Header="Item 2"/>
</ContextMenu>
</TreeView.Resources>
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="ContextMenu" Value="{StaticResource TestMenu}"/>
</Style>
</TreeView.ItemContainerStyle>
<TextBlock>Item 1</TextBlock>
<TextBlock>Item 2</TextBlock>
</TreeView>

Error 1 'System.Windows.Controls.ContextMenu Items.Count:2' is not a valid value for 'Setter.Value'; values derived from Visual or ContentElement are not supported.

What makes this strange is that the exact same thing works just fine when I use a ListView and ListViewItem instead of TreeView.

Any ideas

Thanks in advance,

Rei



Re: Windows Presentation Foundation (WPF) Setting a ContextMenu on a TreeViewItem using TreeView.ItemContainerStyle doesn't work

Drew Marsh

Can you show us the XAML for TestMenu

-Drew





Re: Windows Presentation Foundation (WPF) Setting a ContextMenu on a TreeViewItem using TreeView.ItemContainerStyle doesn't work

Rei Miyasaka

Whoops, sorry about that.

Code Snippet

<TreeView.Resources>
<ContextMenu x:Key="TestMenu">
<MenuItem Header="Item 1"/>
<MenuItem Header="Item 2"/>
</ContextMenu>
</TreeView.Resources>





Re: Windows Presentation Foundation (WPF) Setting a ContextMenu on a TreeViewItem using TreeView.ItemContainerStyle doesn't work

Sam Bent - MSFT

Well, I get the same exception when using ListView/ListViewItem. Or anything else, for that matter.

The exception is saying that you cannot set "tree-valued" properties from a style. Which makes some sense when you think about applying the style to two (or more) targets. They can't share the same tree-value without running into problems: does the tree-value belong to the subtree of target A or to the subtree of target B

However, sharing a ContextMenu doesn't seem so bad, and it's allowed if you use direct values. I'll have to ask the experts why this isn't allowed from a style.






Re: Windows Presentation Foundation (WPF) Setting a ContextMenu on a TreeViewItem using TreeView.ItemContainerStyle doesn't work

Drew Marsh

Hmm, strange. Not sure why off the top of my head, but changing to use DynamicResource instead of StaticResource actually makes it work.

-Drew





Re: Windows Presentation Foundation (WPF) Setting a ContextMenu on a TreeViewItem using TreeView.ItemContainerStyle doesn't work

Rei Miyasaka

Hey, thanks for responding.

Drew Marsh wrote:

Hmm, strange. Not sure why off the top of my head, but changing to use DynamicResource instead of StaticResource actually makes it work.

-Drew

Try putting the ContextMenu right in the <Setter.Value> tag; it still won't work.

Ultimately I want to get this working with a binding to an Attached Property.





Re: Windows Presentation Foundation (WPF) Setting a ContextMenu on a TreeViewItem using TreeView.ItemContainerStyle doesn't work

Sam Bent - MSFT

The feature owners agree that the exception is too restrictive. We'll fix this in a future release.

Using a binding already avoids the exception. In other words, this works:

<Setter Property="ContextMenu" Value="{Binding Source={StaticResource TestMenu}}"/>






Re: Windows Presentation Foundation (WPF) Setting a ContextMenu on a TreeViewItem using TreeView.ItemContainerStyle doesn't work

Drew Marsh

Good to know.

Wouldn't just using DynamicResource, which worked when I tried it as I pointed out above, be the cheapest/simplest approach

Cheers,
Drew





Re: Windows Presentation Foundation (WPF) Setting a ContextMenu on a TreeViewItem using TreeView.ItemContainerStyle doesn't work

Rei Miyasaka

Ah, that works.

Thanks!




Re: Windows Presentation Foundation (WPF) Setting a ContextMenu on a TreeViewItem using TreeView.ItemContainerStyle doesn't work

Sam Bent - MSFT

Drew asked: Wouldn't just using DynamicResource, which worked when I tried it as I pointed out above, be the cheapest/simplest approach

Yes, if that were the whole story. But Rei said "Ultimately I want to get this working with a binding to an Attached Property", which got me looking at bindings.






Re: Windows Presentation Foundation (WPF) Setting a ContextMenu on a TreeViewItem using TreeView.ItemContainerStyle doesn't work

Drew Marsh

Ahhhh... I see. I didn't pick up on that aspect of the conversation, sorry.

Cheers,

Drew