简体   繁体   English

C# WPF MVVM DataGrid MouseBinding 仅用于行

[英]C# WPF MVVM DataGrid MouseBinding for rows only

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.我有一个带有 DataGrid 的 WPF 应用程序,我希望用户能够双击一行以打开一个编辑对话框。

So I added a SelectedItem property in my ViewModel and InputBinding to my Grid.所以我在我的 ViewModel 和InputBinding中添加了一个SelectedItem属性到我的网格。 Now I have a command in my ViewModel which is fired when the user doubleclicks on the grid.现在我的 ViewModel 中有一个命令,当用户双击网格时会触发该命令。 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.因为事件在SelectedItem未更改的情况下被触发。 So this is wrong.所以这是错误的。

My XAML code for the DataGrid:我的 DataGrid 的 XAML 代码:

<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: SelectedItem 属性:

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?如何通过 MVVM 方式解决此问题?

Regards问候

You can set the event for DataGridRow , something like this您可以为DataGridRow设置事件,如下所示

<DataGrid.Resources>
    <Style TargetType="DataGridRow">
        <Setter Property="command:CommandBehavior.Event" Value="MouseDoubleClick"/>
        <Setter Property="command:CommandBehavior.Command" Value="{Binding DataContext.MouseDoubleClickCommand, RelativeSource={RelativeSource FindAncestor, AncestorType=DataGrid}}"/>
    </Style>
</DataGrid.Resources>

Define a CellTemplate and move the MouseBinding to the root element of this one:定义一个CellTemplate并将MouseBinding移动到这个的根元素:

<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.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.CellStyle>
        <Style TargetType="{x:Type DataGridCell}">
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="BorderBrush" Value="Transparent"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type DataGridCell}">
                        <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                            <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                            <Border.InputBindings>
                                <MouseBinding MouseAction="LeftDoubleClick" Command="{Binding DataContext.OnDataGridDoubleClickCommand, 
                                            RelativeSource={RelativeSource AncestorType=DataGrid}}"/>
                            </Border.InputBindings>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
                    <Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                </Trigger>
                <Trigger Property="IsKeyboardFocusWithin" Value="True">
                    <Setter Property="BorderBrush" Value="{DynamicResource {x:Static DataGrid.FocusBorderBrushKey}}"/>
                </Trigger>
                <MultiTrigger>
                    <MultiTrigger.Conditions>
                        <Condition Property="IsSelected" Value="true"/>
                        <Condition Property="Selector.IsSelectionActive" Value="false"/>
                    </MultiTrigger.Conditions>
                    <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightBrushKey}}"/>
                    <Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightBrushKey}}"/>
                    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}}"/>
                </MultiTrigger>
                <Trigger Property="IsEnabled" Value="false">
                    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </DataGrid.CellStyle>
</DataGrid>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM