I have a WPF application with a DataGrid and I want the user be able to doubleclick on a row to open up an edit dialog.
So I added a SelectedItem property in my ViewModel and InputBinding to my Grid. Now I have a command in my ViewModel which is fired when the user doubleclicks on the grid. I get the correct selected item too. So far so good.
The problem is, the event gets also fired when the user clicks on some empty space on the grid (I marked it on the picture)..
The user should not be able to perform a double click action on the empty spaces. Because the event gets fired with the SelectedItem not changed. So this is wrong.
My XAML code for the DataGrid:
<DataGrid Name="dgSafetyFunction" AutoGenerateColumns="False" ItemsSource="{Binding SafetyFunctionList}" Margin="0,0,0,45"
SelectedItem="{Binding Path=SelectedItem, Mode=TwoWay}" IsReadOnly="True" IsSynchronizedWithCurrentItem="True" SelectionUnit="FullRow">
<DataGrid.InputBindings>
<MouseBinding
MouseAction="LeftDoubleClick"
Command="{Binding OnDataGridDoubleClickCommand}"/>
</DataGrid.InputBindings>
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Binding="{Binding ID}"/>
<DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
<DataGridTextColumn Header="Beschreibung" Binding="{Binding Description}"/>
<DataGridTextColumn Header="Projekt ID" Binding="{Binding ProjectID}"/>
</DataGrid.Columns>
</DataGrid>
The SelectedItem property:
private SafetyFunctionModel m_SelectedItem;
public SafetyFunctionModel SelectedItem
{
get
{
return m_SelectedItem;
}
set
{
if (value != m_SelectedItem)
{
m_SelectedItem = value;
OnPropertyChanged("SelectedItem");
}
}
}
How can I fix this the MVVM way?
Regards