Bạn có thể làm điều đó trong XAML bằng cách thêm phong cách mặc định cho DataGridRow dưới phần tài nguyên của mình và tuyên bố sự kiện setter đằng kia:
<DataGrid>
<DataGrid.Resources>
<Style TargetType="DataGridRow">
<EventSetter Event="MouseDoubleClick" Handler="Row_DoubleClick"/>
</Style>
</DataGrid.Resources>
</DataGrid>
HOẶC
Trong trường hợp muốn làm điều đó trong mã ẩn. Đặt x:Name
trên lưới, tạo kiểu theo chương trình và đặt kiểu là RowStyle.
<DataGrid x:Name="dataGrid"/>
và trong mã đằng sau:
Style rowStyle = new Style(typeof(DataGridRow));
rowStyle.Setters.Add(new EventSetter(DataGridRow.MouseDoubleClickEvent,
new MouseButtonEventHandler(Row_DoubleClick)));
dataGrid.RowStyle = rowStyle;
VÀ
Có ví dụ về xử lý sự kiện:
private void Row_DoubleClick(object sender, MouseButtonEventArgs e)
{
DataGridRow row = sender as DataGridRow;
// Some operations with this row
}
Nguồn
2014-04-01 16:37:19
Nó sẽ không thể dễ dàng nhất để đăng ký cho 'myGrid .MouseDoubleClick' và sau đó trong xử lý sự kiện kiểm tra nếu nhấp chuột xảy ra trên một hàng (hoặc ro w được chọn)? – floele