0

我有一个页面和树视图。我正在使用 MVVM。

假设我的页面正在使用我的数据视图模型数据上下文。我的树视图绑定到我的视图模型中的另一个公共对象。现在在我的树项中,我想在页面视图模型中绑定命令。我如何在 xaml 中引用?

下面的代码。

<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>

任何帮助都非常感谢!

4

1 回答 1

1

如果您使用的是 Josh Smith 的RelayCommand课程,那么命令

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

SomeMethod在哪里

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

并且对象o将保持CheckBoxes 状态 ( IsChecked)。现在您要使用的绑定是

<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>

其中通过 objectCommandParameter="{Binding RelativeSource={RelativeSource Self}}"将状态传递给您的命令。IsCheckedo

我希望这有帮助。

于 2013-09-05T09:16:21.800 回答