简体   繁体   中英

Pass the item DataContext in CommandParameter

I have a treeview which is created from ItemsSource of SecondViewModel instances, different from my Window DataContext.

I want to send the ViewModel that belongs to the TreeViewItem via a `CommandParameter.

The window data context is: MyViewModel . The treeviewitems data context is: SecondViewModel

I want to pass the SecondViewModel and not MyViewModel .

Therefore,

CommandParameter ="{Binding}" 

Won't work (as it will send MyViewModel )

Edit: Some Code:

 <TreeView Name="treeView" ItemContainerStyle="{StaticResource TreeViewItemStyle}"  Grid.Row="1" Grid.Column="1">
        <TreeViewItem Header="{Binding ProjectName}">
            <TreeViewItem commandBehaviors:MouseDoubleClick.Command="{Binding SelectOtherTab}" 
                          commandBehaviors:MouseDoubleClick.CommandParameter="{Binding}" //this returns the data context of the window, I want to return the Item Source
                ContextMenu="{StaticResource AddClassMenu}" ItemTemplate="{DynamicResource ClassDataTemplate}" ItemsSource="{Binding ClassCollection}">

How can I send the SecondViewModel ?

EDIT:

I want to enable deleting the current item, but the command never gets called for some reason.

Here's the code:

<TreeViewItem x:Name="treeViewItem"
                ContextMenu="{StaticResource AddClassMenu}" ItemTemplate="{DynamicResource ClassDataTemplate}" ItemsSource="{Binding ClassCollection}">
                <TreeViewItem.ItemContainerStyle>
                    <Style TargetType="TreeViewItem">
              HERE->>          <Setter Property="ContextMenu" Value="{StaticResource RemoveClassMenu}"/>
                        <Setter Property="commandBehaviors:MouseDoubleClick.Command" 
        Value="{Binding ElementName=treeViewItem, Path=DataContext.SelectOtherTab}" />
                        <Setter Property="commandBehaviors:MouseDoubleClick.CommandParameter" 
        Value="{Binding }" />
                    </Style>
</TreeViewItem>

My Context Menu:

 <ContextMenu x:Key="RemoveClassMenu">
    <MenuItem Header="Delete" Command="{Binding ElementName=treeViewItem, Path=DataContext.RemoveClass}" CommandParameter="{Binding}"/>
</ContextMenu>

As mentions before, the command just never gets called. What is the problem with my code?

I think what you're looking to do is set your DoubleClick commands on your child TreeViewItems , not your parent TreeViewItem which sets the ItemsSource

Right now your XAML is saying to build a parent TreeViewItem , and under that build a bunch of child TreeViewItems for each item in ClassCollection . When you double click on the parent TreeViewItem then run the SelectOtherTab command, however there's nothing to specify which child TreeViewItem got clicked.

Here's a simplified view of the XAML you have now.

<TreeView x:Name="treeView">
    <TreeViewItem OnDoubleClick="SelectOtherTab"> <!-- Parent TreeViewItem -->
        <TreeViewItem /><!-- Child TreeViewItems -->
        <TreeViewItem />
        <TreeViewItem />
        ...
    </TreeViewItem>
</TreeView>

Instead you want to attach the Command and CommandParameter properties to each child TreeViewItem , like this:

<TreeViewItem.ItemContainerStyle>
  <Style TargetType="TreeViewItem">
    <Setter Property="commandBehaviors:MouseDoubleClick.Command" 
            Value="{Binding ElementName=treeView, Path=DataContext.SelectOtherTab}" />
    <Setter Property="commandBehaviors:MouseDoubleClick.CommandParameter" 
            Value="{Binding }" />
  </Style>
</TreeViewItem.ItemContainerStyle>

Which will make your simplified XAML look something like this:

<TreeView x:Name="treeView">
    <TreeViewItem> <!-- Parent TreeViewItem -->
        <TreeViewItem OnDoubleClick="SelectOtherTab" /><!-- Child TreeViewItems -->
        <TreeViewItem OnDoubleClick="SelectOtherTab"/>
        <TreeViewItem OnDoubleClick="SelectOtherTab" />
        ...
    </TreeViewItem>
</TreeView>

I'm still a bit confused by why you have a parent TreeViewItem and are building child TreeViewItems using it's ItemsSource , however if that's not necessary you can simplify your VisualTree by eliminating the parent TreeViewItem like this:

<TreeView Name="treeView" 
          ItemsSource="{Binding ClassCollection}"
          ItemContainerStyle="{StaticResource TreeViewItemStyle}"  
          Grid.Row="1" Grid.Column="1">
    <TreeView.Resources>
        <Style TargetType="{x:Type TreeViewItem}">
            <Setter Property="Header" Value="ProjectName" />
            <Setter Property="ContextMenu" Value="{StaticResource AddClassMenu}" />
            <Setter Property="ItemTemplate" Value="{DynamicResource ClassDataTemplate}" />

            <Setter Property="commandBehaviors:MouseDoubleClick.Command" 
                    Value="{Binding ElementName=treeView, Path=DataContext.SelectOtherTab}" />
            <Setter Property="commandBehaviors:MouseDoubleClick.CommandParameter" 
                    Value="{Binding }" />
        </Style>
    </TreeView.Resources>
</TreeView>

Which will make your TreeView look like this:

<TreeView x:Name="treeView">
    <TreeViewItem OnDoubleClick="SelectOtherTab" />
    <TreeViewItem OnDoubleClick="SelectOtherTab"/>
    <TreeViewItem OnDoubleClick="SelectOtherTab" />
    ...
</TreeView>

you must declare a property of type SecondViewModel in MyViewModel.then you can bind the data to inner items too.

CommandParameter ="{Binding SecondViewModelProp.CommandParam}"

if command parameter is defined inside the SecondViewModelProp.

Let me know if I understood something wrong.

 public class MyViewModel
 {
    public SecondViewModel  SecondViewModelProp
    {
        get { return new SecondViewModel(); }
    }

   public MyViewModel()
   {

   }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM