I have a page and tree view. I am using MVVM.

Say my page is using my data viewmodel data context. My tree view is binded to another public object in my view model. Now inside my tree item, I wanted to bind the command in the page view model. How do I refer in the xaml?

code below.

<TreeView  Style="{StaticResource MyNodeStyle}" 
        ItemsSource="{Binding {**Object in Page ViewModel**)}"   
        ItemContainerStyle="{StaticResource TreeViewItemStyle}"  
        ScrollViewer.HorizontalScrollBarVisibility="Hidden"  
        DockPanel.Dock="Bottom" Height="440">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Connections}" 
                        ItemContainerStyle="{StaticResource ResourceKey=TreeViewItemConnectionStyle}" >
    <WrapPanel>
        <CheckBox  VerticalAlignment="Center" 
                Command="{Binding {**Command in Main Page View Model** }}"    
                IsChecked="{Binding Status,  Mode=TwoWay}" 
                Focusable="False"  
                Style="{StaticResource ResourceKey=TreeView_CheckBox_Style}"  >
        </CheckBox>
        <TextBlock Text="{Binding Name}"  Style="{StaticResource ResourceKey=treeTextBoxStyle}" />
    </WrapPanel>

Any help greatly appriciated!

1

There are 1 best solutions below

2
On

If you are using Josh Smith's RelayCommand class, then the command

private RelayCommand updateRootConnection;
public RelayCommand UpdateRootConnection
{
    get {
        return updateRootConnection ?? (updateRootConnection =
            new RelayCommand(o => SomeMethod(o));
    }
}

Where SomeMethod is

public void SomeMethod(object o) { ... }

and the object o will hold the CheckBoxes state (IsChecked). Now the binding you want to use is

<TreeView  Style="{StaticResource MyNodeStyle}"  
           ItemsSource="{Binding {**Object in Page ViewModel**)}"  
           ItemContainerStyle="{StaticResource TreeViewItemStyle}" 
           ScrollViewer.HorizontalScrollBarVisibility="Hidden" 
           DockPanel.Dock="Bottom" 
           Height="440">
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding Connections}" 
                                  ItemContainerStyle="{StaticResource ResourceKey=TreeViewItemConnectionStyle}" >
            <WrapPanel>
                <CheckBox VerticalAlignment="Center" 
                          Command="{Binding UpdateRootConnection}" 
                          CommandParameter="{Binding RelativeSource={RelativeSource Self}}" 
                          IsChecked="{Binding Status,  Mode=TwoWay}" 
                          Focusable="False"  
                          Style="{StaticResource ResourceKey=TreeView_CheckBox_Style}">

                </CheckBox>
                <TextBlock Text="{Binding Name}" 
                           Style="{StaticResource ResourceKey=treeTextBoxStyle}" />
            </WrapPanel>
        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>

where the CommandParameter="{Binding RelativeSource={RelativeSource Self}}" is passing the IsChecked state into your command via the object o.

i hope this helps.