WPF数据网格选择了行点击事件?

时间:2010-06-25 18:47:13

标签: wpf events datagrid

我想在双击WPF DataGrid的选定行时执行一些代码。我知道datagrid有一个MouseDoubleClicked事件,并且它也有一个行选择事件,但我没有看到任何“选择行双击”的事件......

你认为有可能以某种方式捕获这个事件吗?

6 个答案:

答案 0 :(得分:49)

您可以在ItemContainerStyle中添加事件处理程序(这是应用于行的样式):

<DataGrid ... >
    <DataGrid.ItemContainerStyle>
        <Style TargetType="DataGridRow">
            <EventSetter Event="MouseDoubleClick" Handler="Row_DoubleClick"/>
        </Style>
    </DataGrid.ItemContainerStyle>
    ...
</DataGrid>

然后,在处理程序中,您可以检查是否已选中该行

private void Row_DoubleClick(object sender, MouseButtonEventArgs e)
{
    // execute some code
}

答案 1 :(得分:15)

在寻找解决方案时,这个问题出现了,而且由于年龄或我自己的实施,答案都无效。无论哪种方式,这都是适合我的解决方案。

将MouseDoubleClick事件添加到DataGrid

        <DataGrid x:Name="DatagridMovie"
              Width="Auto"
              CanUserAddRows="False"
              CanUserDeleteRows="True"
              IsReadOnly="true"
              ItemsSource="{Binding}"
              MouseDoubleClick="Row_MouseDoubleClick">

并在方法中

private void Row_MouseDoubleClick(object sender, MouseButtonEventArgs e)
                {                
                    // Ensure row was clicked and not empty space
                    DataGridRow row = ItemsControl.ContainerFromElement((DataGrid)sender, e.OriginalSource as DependencyObject) as DataGridRow;
                    if ( row == null ) return;

                    ...
                    Stuff();
                }

到目前为止,我还没有发现任何问题。它没有分享其他人所拥有的问题,这意味着双击标题或空格预先选择的行仍然会导致它运行。

答案 2 :(得分:4)

您可以尝试使用当前单元格更改的事件处理程序,只需单击一下即可,如果这就是您要查找的内容,则不能双击,因为双击可用于启动编辑单元格或整行或任何其他进程:

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);

    }

答案 3 :(得分:0)

为什么不在DoubleClick事件发生时获取SelectedRow属性并对其执行某些操作?如果SelectedRow为null,则表示没有选择Row,因此只返回

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
}

答案 4 :(得分:0)

ItemContainerStyle没有最佳解决方案,建议使用RowStyle

在您的XAML中:

<DataGrid.RowStyle>
    <Style TargetType="DataGridRow">        
        <EventSetter Event="MouseDoubleClick" Handler="DataGridRow_MouseDoubleClick"/>
    </Style>
</DataGrid.RowStyle>

在您的代码中:

private void DataGridRow_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    //your logic here
}

答案 5 :(得分:0)

使用数据绑定和MVVM,您将像这样执行一键式事件(=行的selectedItem):

    <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.*/

    }
        }