Reputation: 1
I want to have function in WPF:Double click on row in Data Grid will show cells in window. How can I do it that?I programm so but I never have double click event. Can somebody help me? Thanks.
<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>
Upvotes: 0
Views: 549
Reputation: 174
You can subscribe on PreviewMouseDoubleClick event.
<Style TargetType="DataGridRow">
<EventSetter Event="PreviewMouseDoubleClick" Handler="OnDoubleClick"/>
</Style>
Handler:
private void OnDoubleClick(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
System.Diagnostics.Debug.WriteLine("DoubleClick!");
}
Upvotes: 0
Reputation: 189
Try this
So, for instance, your XAML might look something like this:
<SomeControl MouseDown="MyMouseDownHandler">
...
</SomeControl>
code behind the click event..
private void MyMouseDownHandler(object sender, MouseButtonEventArgs e)
{
if (e.ClickCount == 2)
{
//Handle double-click
}
}
Upvotes: 1
Reputation: 149
Just create your own dependency property DataGridDoubleClickProperty where you attach handler for DataGrid.MouseDoubleClick event
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);
}
}
}
}
In your View you use Binding to map this DependencyProperty to Command
<Grid DataContext="{StaticResource viewModel}">
<DataGrid AutoGenerateColumns="True"
ItemsSource="{Binding Data}"
SelectedItem="{Binding SelectedItem}"
clr:Commands.DataGridDoubleClickCommand="{Binding DataGridDoubleClick}"
/>
</Grid>
DataGridDoubleClick is ICommand property in your ViewModel class
Upvotes: 0