【发布时间】:2018-11-13 10:55:41
【问题描述】:
我有一个视图模型,其中包含一组对象和一堆命令。
public class MainWindowVM : NotifyPropertyChangedBase
{
private CollectionViewSource employeeViewSource;
private ICommand cmdOpenDetailEmployee;
public MainWindowVM()
{
nsDataProviderEmployees = new NSDataProvider();
employeeViewSource = new CollectionViewSource();
cmdOpenDetailEmployee = new RelayCommand<object>((parameter) => {...});
this.LoadData();
}
public CollectionViewSource EmployeeViewSource => employeeViewSource;
public ICommand CmdOpenDetailEmployee => cmdOpenDetailEmployee;
}
在我的应用程序中,我想在显示员工的数据网格的上下文菜单中使用此命令。
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MyApp.UI"
xmlns:DataModel="clr-namespace:MyApp.DataModel;assembly=MyApp.DataModel" x:Class="MyApp.UI.MainWindow"
xmlns:vm="clr-namespace:MyApp.UI.ViewModels"
mc:Ignorable="d"
Title="MyApp - Main" Height="751.826" Width="1111.005" Loaded="Window_Loaded" Icon="Resources/MyApp.ico">
<Window.DataContext>
<vm:MainWindowVM />
</Window.DataContext>
<Grid x:Name="grdMain">
<DataGrid DataContext="{Binding Path=EmployeeViewSource}" x:Name="employeeDataGrid" EnableRowVirtualization="True" ItemsSource="{Binding}" Margin="10,77,10,0" RowDetailsVisibilityMode="VisibleWhenSelected">
<DataGrid.ContextMenu>
<ContextMenu DataContext="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}">
<MenuItem Header="OpenDetail..."
Command="{Binding CmdOpenDetailEmployee}"
CommandParameter="{Binding}"/>
</ContextMenu>
</DataGrid.ContextMenu>
<DataGrid.Columns>...</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
问题是我无法想出一个绑定组合,既可以让我使用EmployeeViewSource 属性或我的 ViewModel 作为网格的 DataContext,又可以使用我的CmdOpenDetailEmployee 属性ViewModel 作为我的 ContextMenu 和 MenuItems 的 DataContext。
根据我找到的所有帖子,应该可以工作,但是当我单击菜单项时,该命令没有执行。
【问题讨论】:
-
首先,看看问题到底出在绑定还是命令上。检查是否可以找到绑定错误(另请参阅此处:stackoverflow.com/questions/8850143/…)。如果实际调用了命令的 Execute 方法,还要检查调试器。从这个问题来看,真的不可能告诉更多......
-
我收回我之前评论的最后一句话。 :-) 想一想,它可能是使用
FindAncestor的绑定。由于上下文菜单是一个弹出窗口,它可能没有主窗口作为祖先(我在这里不是 100% 确定)。绑定很可能只是找到上下文菜单的弹出窗口元素(如果有的话)。尝试使用ElementName的绑定来代替(当然,你需要给你的主窗口一个名字),看看会发生什么...... -
@elgonzo 这是来自输出的错误
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Window', AncestorLevel='1''. BindingExpression:(no path); DataItem=null; target element is 'MenuItem' (Name=''); target property is 'Command' (type 'ICommand')我的命令的执行方法没有被调用。而且我也没有设法与 ElementName 绑定工作:Command="{Binding ElementName=mainWindow, Path=CmdOpenDetailEmployee}"
标签: c# wpf data-binding