WPF 数据网格选定行单击事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3120616/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF datagrid selected row clicked event ?
提问by MadSeb
I want to execute some code when a a selected row of the WPF DataGrid is double clicked. I know that the datagrid has a MouseDoubleClicked event and that it also has a row selected event but I don't see any event for "selected row double clicked" ...
我想在双击 WPF DataGrid 的选定行时执行一些代码。我知道数据网格有一个 MouseDoubleClicked 事件,它也有一个行选择事件,但我没有看到“双击所选行”的任何事件......
Do you think it's possible to capture this event somehow ?
你认为有可能以某种方式捕捉到这个事件吗?
回答by Thomas Levesque
you can add the event handler in the ItemContainerStyle
(which is the style applied to a row) :
您可以在ItemContainerStyle
(这是应用于行的样式)中添加事件处理程序:
<DataGrid ... >
<DataGrid.ItemContainerStyle>
<Style TargetType="DataGridRow">
<EventSetter Event="MouseDoubleClick" Handler="Row_DoubleClick"/>
</Style>
</DataGrid.ItemContainerStyle>
...
</DataGrid>
Then, in the handler, you can check if the row is selected
然后,在处理程序中,您可以检查该行是否被选中
private void Row_DoubleClick(object sender, MouseButtonEventArgs e)
{
// execute some code
}
回答by Gilgamesh
This question came up for me while looking for a solution and the answers didn't work, whether due to age or my own implementation. Either way, here is the solution that worked for me.
我在寻找解决方案时提出了这个问题,但无论是由于年龄还是我自己的实施,答案都不起作用。无论哪种方式,这是对我有用的解决方案。
Add the MouseDoubleClick event to the DataGrid
将 MouseDoubleClick 事件添加到 DataGrid
<DataGrid x:Name="DatagridMovie"
Width="Auto"
CanUserAddRows="False"
CanUserDeleteRows="True"
IsReadOnly="true"
ItemsSource="{Binding}"
MouseDoubleClick="Row_MouseDoubleClick">
and in the method
并在方法中
private void Row_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
// Ensure row was clicked and not empty space
var row = ItemsControl.ContainerFromElement((DataGrid)sender,
e.OriginalSource as DependencyObject) as DataGridRow;
if ( row == null ) return;
… Stuff();
}
So far I haven't noticed any problems with it. It doesn't share the problem that others have that means double clicking a header or empty space with a row selected beforehand would still cause it to run.
到目前为止,我还没有注意到它有任何问题。它不存在其他人遇到的问题,这意味着双击标题或空白区域并预先选择一行仍然会导致它运行。
回答by 0x01Brain
You could try current cell changed event handler it works only with one click and not double click if thats what your looking for, since double click can be used to for initiating editing cell or entire row or for any other process:
您可以尝试当前单元格更改事件处理程序,它仅通过单击即可工作,如果这就是您要查找的内容,则不能双击,因为双击可用于启动编辑单元格或整行或任何其他过程:
private void datagrid_CurrentCellChanged(object sender, EventArgs e)
{
int selected_index = datagrid.SelectedIndex + 1;
// this is used for debugging and testing.
//MessageBox.Show("The index of the row for the clicked cell is " + selected_index);
}
回答by Darlan Dieterich
The ItemContainerStyle
do not have best solution, suggest use the RowStyle
:
在ItemContainerStyle
没有最佳的解决方案,建议使用RowStyle
:
In your XAML:
在您的 XAML 中:
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<EventSetter Event="MouseDoubleClick" Handler="DataGridRow_MouseDoubleClick"/>
</Style>
</DataGrid.RowStyle>
In your Code:
在您的代码中:
private void DataGridRow_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
//your logic here
}
回答by timunix
With data binding and MVVM you would do one-click event (=selectedItem of row) like this:
使用数据绑定和 MVVM,你可以像这样执行一键事件(=selectedItem of row):
<Datagrid ItemsSource="{Binding YourObservableCollectionProperty}"
SelectedItem="{Binding YourSelectedItemProperty}">
//more...
</Datagrid>
CodeBehind:
代码隐藏:
public partial class YourClass : Window
{
public YourClass()
{
InitializeComponent();
this.DataContext = new YourClassViewModel();
}
}
ViewModel:
视图模型:
public class YourClassViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public virtual void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private ObservableCollection<YourModelClass> _yourObservableCollectionProperty;
public ObservableCollection<YourModelClass> YourObservableCollectionProperty
{
get { return _yourObservableCollectionProperty; }
set
{
_yourObservableCollectionProperty = value;
OnPropertyChanged("YourObservableCollectionProperty");
}
}
private YourModelClass _yourSelectedItemProperty;
public YourModelClass YourSelectedItemProperty
{
get { return _yourSelectedItemProperty; }
set
{
_yourSelectedItemProperty = value;
OnPropertyChanged("YourSelectedItemProperty");
}
}
//Constructor
public YourClassViewModel()
{
/*Take your ModelClass instance and ObservableCollection instance here
and play around with them or move them into a method. Normally your
observablecollection is the itemssource of your datagrid and your selecteditem
is your modelclass.*/
}
}
回答by Carlo
Why don't you get the SelectedRow property while the DoubleClick event happens and do something with it? If the SelectedRow is null, it means no Row is selected so just return
为什么在 DoubleClick 事件发生时不获取 SelectedRow 属性并对其进行处理?如果 SelectedRow 为空,则表示未选择任何行,因此只需返回
private void Grid_DoubleClick(object sender, RoutedEventArgs e)
{
if(grid.SelectedRow == null)
return; // return if there's no row selected
// do something with the Selected row here
}