我想在WPF中使用函数:双击数据网格中的行将显示 窗口中的细胞。我怎么能这样做?我编程,但我从来没有双击事件。有人能帮助我吗? 感谢。
<DataGrid AutoGenerateColumns="False" HorizontalAlignment="Left"Margin="14,55,0,46"
Name="dataGridCustomers" Width="575" ItemsSource=" {Binding Path=LoadDataBinding}"
CanUserResizeRows="False">
<DataGrid.RowStyle>
<Style TargetType="{x:Type DataGridRow}">
<EventSetter Event="MouseDoubleClick" Handler="dataGridCustomers_MouseDoubleClick"/>
</Style>
</DataGrid.RowStyle>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=DEPARTMENT_ID}" Header="DepartmentID" Width="100"
IsReadOnly="True" />
<DataGridTextColumn Binding="{Binding Path=DEPARTMENT_NAME}" Header="Department name" Width="100"
IsReadOnly="True" />
<DataGridTextColumn Binding="{Binding Path=LOCATION}" Header="Location" Width="150"
IsReadOnly="True" />
</DataGrid.Columns>
</DataGrid>
答案 0 :(得分:1)
试试这个
因此,例如,您的XAML可能如下所示:
<SomeControl MouseDown="MyMouseDownHandler">
...
</SomeControl>
点击事件背后的代码..
private void MyMouseDownHandler(object sender, MouseButtonEventArgs e)
{
if (e.ClickCount == 2)
{
//Handle double-click
}
}
答案 1 :(得分:0)
只需创建自己的依赖项属性DataGridDoubleClickProperty,其中为DataGrid.MouseDoubleClick事件附加处理程序
public static class Commands
{
public static readonly DependencyProperty DataGridDoubleClickProperty =
DependencyProperty.RegisterAttached("DataGridDoubleClickCommand", typeof ( ICommand ), typeof ( Commands ),
new PropertyMetadata(new PropertyChangedCallback(AttachOrRemoveDataGridDoubleClickEvent)));
public static ICommand GetDataGridDoubleClickCommand(DependencyObject obj)
{
return (ICommand) obj.GetValue(DataGridDoubleClickProperty);
}
public static void SetDataGridDoubleClickCommand(DependencyObject obj, ICommand value)
{
obj.SetValue(DataGridDoubleClickProperty, value);
}
public static void AttachOrRemoveDataGridDoubleClickEvent(DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
DataGrid dataGrid = obj as DataGrid;
if ( dataGrid != null )
{
ICommand cmd = (ICommand) args.NewValue;
if ( args.OldValue == null && args.NewValue != null )
{
dataGrid.MouseDoubleClick += ExecuteDataGridDoubleClick;
}
else if ( args.OldValue != null && args.NewValue == null )
{
dataGrid.MouseDoubleClick -= ExecuteDataGridDoubleClick;
}
}
}
private static void ExecuteDataGridDoubleClick(object sender, MouseButtonEventArgs args)
{
DependencyObject obj = sender as DependencyObject;
ICommand cmd = (ICommand) obj.GetValue(DataGridDoubleClickProperty);
if ( cmd != null )
{
if ( cmd.CanExecute(obj) )
{
cmd.Execute(obj);
}
}
}
}
在视图中,您使用Binding将此DependencyProperty映射到Command
<Grid DataContext="{StaticResource viewModel}">
<DataGrid AutoGenerateColumns="True"
ItemsSource="{Binding Data}"
SelectedItem="{Binding SelectedItem}"
clr:Commands.DataGridDoubleClickCommand="{Binding DataGridDoubleClick}"
/>
</Grid>
DataGridDoubleClick是ViewModel类中的ICommand属性
答案 2 :(得分:0)
您可以订阅PreviewMouseDoubleClick事件。
<Style TargetType="DataGridRow">
<EventSetter Event="PreviewMouseDoubleClick" Handler="OnDoubleClick"/>
</Style>
处理程序:
private void OnDoubleClick(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
System.Diagnostics.Debug.WriteLine("DoubleClick!");
}