【发布时间】: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