views:

183

answers:

1

Hi, I'm having some trouble with a databound TreeView in WPF, basically I want a context menu to be databound to an IEnumerable property on my TreeViewItem ViewModel, this is what I'm trying to do in the of each TreeViewItem:

<Setter Property="ContextMenu">
<Setter.Value>
 <ContextMenu ItemsSource="{Binding ContextMenu}" />
</Setter.Value>

but it gives me an exception when loading the xaml saying it can't set ContextMenu on System.Object or something along those lines.

Can anyone shed some light on this?

Thanks

+1  A: 

declare it as resource in your style and then assign it to Value as StaticResource

<Style>
  <Style.Resources>
    <ContextMenu x:Key="contextmenustyle" ItemsSource="{Binding ContextMenu}" />
  </Style.Resources>
  <Setter Property="ContextMenu" Value="{StaticResource contextmenustyle}">
</Style>
viky
ahh, excellent.. Any idea why it isn't possible how I was doing it? Seems like I'm not doing it wrong.. they should both produce the same result?
Michael Baldry
check this for detailshttp://blogs.msdn.com/mikehillberg/archive/2006/09/20/SharingAndSetterValue.aspx
viky