![](/img/trans.png)
[英]How to programmatically add a double-click event to a WPF DataGrid Row?
[英]WPF DataGrid row double click event programmatically
我需要以編程方式創建一個DataGrid,並需要向其添加雙擊行事件。 這是如何在C#中完成的? 我找到了這個;
myRow.MouseDoubleClick += new RoutedEventHandler(Row_DoubleClick);
雖然這對我不起作用,因為我將DataGrid.ItemsSource
綁定到集合而不是手動添加行。
你可以在XAML中通過在其資源部分下添加DataGridRow的默認樣式並在那里聲明事件設置器來實現:
<DataGrid>
<DataGrid.Resources>
<Style TargetType="DataGridRow">
<EventSetter Event="MouseDoubleClick" Handler="Row_DoubleClick"/>
</Style>
</DataGrid.Resources>
</DataGrid>
要么
如果想在代碼后面做。 在網格上設置x:Name
,以編程方式創建樣式並將樣式設置為RowStyle。
<DataGrid x:Name="dataGrid"/>
並在代碼后面:
Style rowStyle = new Style(typeof(DataGridRow));
rowStyle.Setters.Add(new EventSetter(DataGridRow.MouseDoubleClickEvent,
new MouseButtonEventHandler(Row_DoubleClick)));
dataGrid.RowStyle = rowStyle;
和
有事件處理程序的例子:
private void Row_DoubleClick(object sender, MouseButtonEventArgs e)
{
DataGridRow row = sender as DataGridRow;
// Some operations with this row
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.