【问题标题】:How to specify the binding path of parent item context from child tree view item which is bounded to another data context in mvvm如何从绑定到 mvvm 中另一个数据上下文的子树视图项中指定父项上下文的绑定路径
【发布时间】:2013-09-05 07:35:09
【问题描述】:

我有一个页面和树视图。我正在使用 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>

非常感谢任何帮助!

【问题讨论】:

  • 只需使用您要使用的ICommand 的名称...
  • 我用过。当我单击检查框时它没有触发,当我看到 wpf 检查器时,它显示为 BindingExpression 路径错误。
  • 显示命令的代码。你听说过 AttachedCommands 吗?
  • 是的。这就是我所做的。 Command="{Binding UpdateRootConnection} 和我的视图模型有 /// /// 要导入 > 按钮处理程序 /// public ICommand UpdateRootConnection { get { return new RelayCommand(UpdateRootConnectionAPI, CanUpdateConnectionsExecute); } }
  • 你的问题还是不清楚!检查this

标签: xaml mvvm wpf-4.0 treeviewitem


【解决方案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>

CommandParameter="{Binding RelativeSource={RelativeSource Self}}" 通过对象oIsChecked 状态传递给您的命令。

我希望这会有所帮助。

【讨论】:

  • 谢谢。但实际的问题是当我单击复选框时它没有与 ICommand 连接。我遵循了同样的方法,但仍然与命令挂钩。有什么帮助吗?
  • 你说的没有道理。我已经向您展示了如何构建绑定命令以及如何使用 XAML 绑定调用该命令。怎么了?现在发生了什么?使用我的示例,什么不起作用?
猜你喜欢
  • 2011-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-31
  • 1970-01-01
  • 2011-04-22
相关资源
最近更新 更多