首页 新闻 会员 周边

WPF MVVM DataGrid中 滑动鼠标滚轮会触发 checkbox的Unchecked事件

0
悬赏园豆:40 [待解决问题]

在UI界面中,勾选第一条数据,无论怎样滑动滚轮 ,不会出发 UnChecked绑定的Command

但勾选第一条数据后,在勾选任务位置的数据,然后滑动滚轮,就会触发 UnChecked绑定的Command事件

xmal如下:

<DataGrid Margin="3" 
                  ItemsSource="{Binding TableStructCheckDtos}"
                  x:Name="DataGrids" GridLinesVisibility="None" BorderThickness="0" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTemplateColumn>
                    <DataGridTemplateColumn.Header>
                        <CheckBox  IsChecked="{Binding DataContext.AllSelected,Mode=TwoWay
                            ,UpdateSourceTrigger=PropertyChanged,RelativeSource={RelativeSource AncestorType=local:PageTableStructCheck,AncestorLevel=1}}">
                            <i:Interaction.Triggers>
                                <i:EventTrigger EventName="Checked">
                                    <i:InvokeCommandAction 
                                        Command="{Binding DataContext.AllSelectedChangedCallback,RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" 
                                        CommandParameter="{Binding AllSelectedx}"></i:InvokeCommandAction>
                                </i:EventTrigger>
                                <i:EventTrigger EventName="Unchecked">
                                    <i:InvokeCommandAction 
                                        Command="{Binding DataContext.UnAllSelectedChangedCallback,RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" 
                                        CommandParameter="{Binding AllSelected}"></i:InvokeCommandAction>
                                </i:EventTrigger>
                            </i:Interaction.Triggers>
                        </CheckBox>
                    </DataGridTemplateColumn.Header>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox x:Name="cbXh" VerticalAlignment="Center" 
                                      IsChecked="{Binding IsChecked,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
                                <i:Interaction.Triggers>
                                    <i:EventTrigger EventName="Checked">
                                        <i:InvokeCommandAction 
                                            Command="{Binding DataContext.SelectedChangedCallback,RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
                                                CommandParameter="{Binding DataIndex}"
                                            ></i:InvokeCommandAction>
                                    </i:EventTrigger>
                                    <i:EventTrigger EventName="Unchecked">
                                        <i:InvokeCommandAction 
                                            Command="{Binding DataContext.UnSelectedChangedCallback,RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
                                            CommandParameter="{Binding DataIndex}"
                                            ></i:InvokeCommandAction>
                                    </i:EventTrigger>
                                </i:Interaction.Triggers>
                            </CheckBox>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTextColumn Header="源数据表"  Binding="{Binding SourceTableName}" IsReadOnly="True" Width="400"/>
                <DataGridTextColumn Header="目标数据表" Binding="{Binding TargetTableName}" IsReadOnly="True" Width="400"/>
            </DataGrid.Columns>
        </DataGrid>
丨渊丨的主页 丨渊丨 | 初学一级 | 园豆:71
提问于:2023-03-21 18:52
< >
分享
所有回答(1)
0

在WPF MVVM DataGrid中,滑动鼠标滚轮会触发checkbox的Unchecked事件的问题,可能是由于DataGrid的滚动机制导致的。可以尝试在DataGrid的ScrollViewer上添加PreviewMouseWheel事件,然后在事件处理程序中手动处理鼠标滚轮事件,避免事件冒泡到checkbox上。具体实现可以参考以下代码:

<DataGrid ScrollViewer.PreviewMouseWheel="DataGrid_PreviewMouseWheel" ...>
...
</DataGrid>

private void DataGrid_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
e.Handled = true;
var eventArg = new MouseWheelEventArgs(e.MouseDevice, e.Timestamp, e.Delta)
{
RoutedEvent = UIElement.MouseWheelEvent,
Source = sender
};
var parent = ((Control)sender).Parent as UIElement;
parent?.RaiseEvent(eventArg);
}

这样可以避免滚动事件冒泡到checkbox上,从而避免触发checkbox的Unchecked事件。

十四年新* | 园豆:239 (菜鸟二级) | 2023-03-23 14:32
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册