在我的视图中,我有一个DataGrid
,它存储2个递减类型的对象。每一行都有一个带有连接到ViewModel的命令的按钮。在ViewModel中,我需要找出哪种类型的对象已被选中。WPF MVVM - 通过ViewModel访问视图中DataGrid的DependencyProperty
问题是什么是在ViewModel中从Execute
命令方法访问的SelectedItem
属性的最好和简单方法?
到目前为止,我这样做是这样的:
var window = Application.Current.Windows.OfType<Window>()
.SingleOrDefault(x => x.IsActive);
var dataGrid = (DataGrid) window.FindName("MyGridName");
...
更新 - XAML中:
<DataGrid Name="MyGridName" ItemsSource="{Binding Elements}"
AutoGenerateColumns="False" CanUserAddRows="False"
CanUserDeleteRows="False" IsReadOnly="True">
<DataGrid.Columns>
<DataGridTemplateColumn Width="auto">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Name="OptionsBtn" Margin="5" Width="auto"
Height="30" Content="Options"
Command="{Binding ElementName=ElementsViewWindow,
Path=DataContext.ShowOptionsMenuCommand}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
绑定了'你的网格SelectedItem'一些VM属性或使用'CommandParameters'和停止不管你正在尝试做... – user3292642
显示您的Xaml中的命令以及如何绑定它。我很高兴能向您展示如何使用该命令参数方法! – CHS
'' – XAMlMAX