I have a usercontrol with a Tree inside it with, the command property binds to the DataType and not the DataContext.
How can I redirect the binding to go to the DataContext and Not the DataType ? Also Out of curiosity how would I bind to the UserControl’s DataContext instead of the Tree’s DataContext ?
Here is the code in question:
<HierarchicalDataTemplate
DataType="{x:Type viewModel:UsersViewModel}"
ItemsSource="{Binding Children}"
>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding UserName}">
<TextBlock.ContextMenu>
<ContextMenu>
<MenuItem Header="Edit" Command="{Binding EditCommand}" CommandParameter="{Binding UserName}"/>
<MenuItem Header="Delete"/>
</ContextMenu>
</TextBlock.ContextMenu>
</TextBlock>
</StackPanel>
</HierarchicalDataTemplate>
It seems to bind to the UsersViewModel and Not the DataContext(AllUsersViewModel).
This is the entire piece of XAML Just in case:
<Grid Width="150">
<TreeView ItemsSource="{Binding Users}" DataContext="{Binding allUsersViewModel}">
<TreeView.ItemContainerStyle>
<!--
This Style binds a TreeViewItem to a TreeViewItemViewModel.
-->
<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="Normal" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="FontWeight" Value="Bold" />
</Trigger>
</Style.Triggers>
</Style>
</TreeView.ItemContainerStyle>
<TreeView.Resources>
<ContextMenu x:Key="CategoryMenu">
<MenuItem Header="Add Subcategory" Command="New">
</MenuItem>
<MenuItem Header="Remove Category" Command="Delete">
</MenuItem>
</ContextMenu>
<HierarchicalDataTemplate
DataType="{x:Type viewModel:UsersViewModel}"
ItemsSource="{Binding Children}"
>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding UserName}">
<TextBlock.ContextMenu>
<ContextMenu>
<MenuItem Header="Edit" Command="{Binding EditCommand}" CommandParameter="{Binding UserName}"/>
<MenuItem Header="Delete"/>
</ContextMenu>
</TextBlock.ContextMenu>
</TextBlock>
</StackPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate
DataType="{x:Type viewModel:PermissionCategoryViewModel}"
ItemsSource="{Binding Children}"
>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding PermissionCategoryName}" />
</StackPanel>
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type viewModel:PermissionViewModel}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding PermissionName}" />
</StackPanel>
</DataTemplate>
</TreeView.Resources>
</TreeView>
</Grid>
Thanks for the Help!
EDIT
Well I tried a few things, but it didnt work out =(. Nothing Happens
I should have mentioned that my MainWindow Has the DataContext and the UserControl Inherits it by placing the UserControl Inside of it.
<Window.DataContext>
<viewModel:MainViewModel/>
</Window.DataContext>
I tried these different things
Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:AllUsers}}, Path=DataContext.EditCommand}"
and
Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:MainWindow}}, Path=DataContext.EditCommand}"
and
Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:MainWindow}}, Path=DataContext.EditCommand}"
and
Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:AllUsers}}, Path=DataContext.EditCommand}"
Lastly
Command="{Binding PlacementTarget.DataContext.EditCommand,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}"
You can use a
RelativeSourcebinding to find the TreeView, then bind to it’s DataContextIf you ever want to bind to your UserControl, you can use the same type of binding:
Note that
RelativeSourcebindings will return a reference to the UI object, not it’sDataContext, so if you want to bind to something in theDataContextyou have to specifyPath=DataContext.SomeValue