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

Mad*_*Seb 34 wpf events datagrid

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

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

Tho*_*que 49

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

<DataGrid ... >
    <DataGrid.ItemContainerStyle>
        <Style TargetType="DataGridRow">
            <EventSetter Event="MouseDoubleClick" Handler="Row_DoubleClick"/>
        </Style>
    </DataGrid.ItemContainerStyle>
    ...
</DataGrid>
Run Code Online (Sandbox Code Playgroud)

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

private void Row_DoubleClick(object sender, MouseButtonEventArgs e)
{
    // execute some code
}
Run Code Online (Sandbox Code Playgroud)

  • @ThomasLeveseque是否有针对行或单元格的单击事件? (3认同)
  • 我发现将它放在“&lt;DataGrid.RowStyle&gt;”中可以工作,而“ItemContainerStyle”则不能。 (2认同)

小智 17

在寻找解决方案时,我提出了这个问题,答案无效,无论是因为年龄还是我自己的实施.无论哪种方式,这是适合我的解决方案.

将MouseDoubleClick事件添加到DataGrid

        <DataGrid x:Name="DatagridMovie"
              Width="Auto"
              CanUserAddRows="False"
              CanUserDeleteRows="True"
              IsReadOnly="true"
              ItemsSource="{Binding}"
              MouseDoubleClick="Row_MouseDoubleClick">
Run Code Online (Sandbox Code Playgroud)

并在方法中

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();
                }
Run Code Online (Sandbox Code Playgroud)

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

  • 你可能想在返回之前做`e.handled = True;` (2认同)

tim*_*nix 5

使用数据绑定和 MVVM,您可以执行一键事件(=行的selectedItem),如下所示:

    <Datagrid ItemsSource="{Binding YourObservableCollectionProperty}" 
        SelectedItem="{Binding YourSelectedItemProperty}"> 
     //more...      
    </Datagrid>
Run Code Online (Sandbox Code Playgroud)

隐藏代码:

public partial class YourClass : Window
    {
        public YourClass()
        {
            InitializeComponent();
            this.DataContext = new YourClassViewModel();                      
        }
}
Run Code Online (Sandbox Code Playgroud)

视图模型:

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

    }
        }
Run Code Online (Sandbox Code Playgroud)