C# WPF MVVM-从ViewModel访问视图中DataGrid的DependencyProperty
在我看来,我有一个C# WPF MVVM-从ViewModel访问视图中DataGrid的DependencyProperty,c#,wpf,mvvm,viewmodel,dependency-properties,C#,Wpf,Mvvm,Viewmodel,Dependency Properties,在我看来,我有一个DataGrid,它存储两种递减类型的对象。每行都有一个按钮,该按钮带有连接到ViewModel的命令。在ViewModel中,我需要找出选择了哪种类型的对象 问题是,从ViewModel中的Execute命令方法访问DataGrid属性的SelectedItem的最佳简单方法是什么 到目前为止,我是这样做的: var window = Application.Current.Windows.OfType<Window>() .SingleOrDefault
DataGrid
,它存储两种递减类型的对象。每行都有一个按钮,该按钮带有连接到ViewModel的命令。在ViewModel中,我需要找出选择了哪种类型的对象
问题是,从ViewModel中的Execute
命令方法访问DataGrid
属性的SelectedItem
的最佳简单方法是什么
到目前为止,我是这样做的:
var window = Application.Current.Windows.OfType<Window>()
.SingleOrDefault(x => x.IsActive);
var dataGrid = (DataGrid) window.FindName("MyGridName");
...
var window=Application.Current.Windows.OfType()
.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>
如果您采用正确的MVVM方法,这是非常容易做到的。您只需定义将绑定到
DataGrid
的ItemsSource
的实体的项集合,以及将绑定到DataGrid
的SelectedItem
的属性。然后在命令中,您只需引用模型的selected item属性即可访问数据网格中的selected item
下面是一个使用MVVM Light的示例实现。首先定义实体的可观察集合:
public const string ItemsCollectionPropertyName = "ItemsCollection";
private ObservableCollection<DataItem> _itemsCollection = null;
public ObservableCollection<DataItem> ItemsCollection
{
get
{
return _itemsCollection;
}
set
{
if (_itemsCollection == value)
{
return;
}
_itemsCollection = value;
RaisePropertyChanged(ItemsCollectionPropertyName);
}
}
然后定义一个命令来处理业务逻辑:
private ICommand _doWhateverCommand;
public ICommand DoWhateverCommand
{
get
{
if (_doWhateverCommand == null)
{
_doWhateverCommand = new RelayCommand(
() => { /* do your stuff with SelectedItem here */ },
() => { return SelectedItem != null; }
);
}
return _doWhateverCommand;
}
}
最后,创建视图元素并将其绑定到ViewModel
:
<DataGrid ItemsSource="{Binding ItemsCollection}" SelectedItem="{Binding SelectedItem}" AutoGenerateColumns="True" />
<Button Content="Do stuff" Command="{Binding DoWhateverCommand}" />
问题是,在ViewModel中,通过执行命令函数访问DataGrid的SelectedItem属性的最佳简单方法是什么
只需在视图模型类中添加一个属性,其中定义了showoptions菜单命令属性,并将DataGrid
的SelectedItem
属性绑定到此属性:
<DataGrid Name="MyGridName" ItemsSource="{Binding Elements}" SelectedItem="{Binding SelectedElement}" ... >
将网格的SelectedItem
绑定到某个VM属性,或者使用CommandParameters
并停止尝试执行的任何操作…显示Xaml中的命令以及如何绑定该命令。我很高兴向您展示如何使用命令参数方法<代码>
<DataGrid Name="MyGridName" ItemsSource="{Binding Elements}" SelectedItem="{Binding SelectedElement}" ... >
<Button Name="OptionsBtn" ... Content="Options"
Command="{Binding ElementName=ElementsViewWindow, Path=DataContext.ShowOptionsMenuCommand}"
CommandParameter="{Binding}" />