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) { ... }

オブジェクトはes 状態 ( )oを保持します。使用するバインディングは次のとおりです。CheckBoxIsChecked

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

ここで、オブジェクトを介してコマンドに状態をCommandParameter="{Binding RelativeSource={RelativeSource Self}}"渡しています。IsCheckedo

これが役立つことを願っています。

于 2013-09-05T09:16:21.800 に答える