wpf DataGrid 实现行重复点击

wpf DataGrid 实现行重复点击事件

由于行选中事件,选中后不会重复触发选中,只能取消选中,才能再次点击行,所以做了个鼠标松开事件,用于实现特定业务逻辑,具体实现方式如下,DataGridRow.MouseUp事件不会自动补全出来,但是可以使用:

<DataGrid AutoGenerateColumns="False"
        IsReadOnly="True"  
        ItemsSource="{Binding TestDataList, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
        DataGridRow.MouseUp="DataGrid_MouseUp">  
    <DataGrid.Columns>
        <DataGridTextColumn Header="标题"
                            Binding="{Binding Title}"
                            Width="*" />
        <DataGridTextColumn Header="描述"
                            Binding="{Binding Description}"
                            Width="*" />
    </DataGrid.Columns>
</DataGrid>

private void DataGrid_MouseUp(object sender, MouseButtonEventArgs e)
{
    DataGrid dataGrid = (DataGrid)sender;
    var pos = e.GetPosition(dataGrid);
    var result = VisualTreeHelper.HitTest(dataGrid, pos);
    if (result == null) { return; }

    var rowItem = VisualHelper.FindParent<DataGridRow>(result.VisualHit);
    if (rowItem == null) { return; }

    DataViewModel dataView = rowItem.DataContext as DataViewModel;

    if (dataView != null)
    {
        /* 处理业务逻辑 */
    }
}

通过扩展方法转换为Command方式

添加DataGrid的扩展方法:

public static class DataGridExtensions
{
    public static readonly DependencyProperty RowMouseUpCmdProperty =
        DependencyProperty.RegisterAttached(
            "RowMouseUpCmd",
            typeof(ICommand),
            typeof(DataGridExtensions),
            new FrameworkPropertyMetadata(default(ICommand), new PropertyChangedCallback(RowMouseUpCmd_ChangesCallback))
    );

    public static void SetRowMouseUpCmd(DependencyObject element, ICommand value)
        => element.SetValue(RowMouseUpCmdProperty, value);

    public static ICommand GetRowMouseUpCmd(DependencyObject element)
        => (ICommand)element.GetValue(RowMouseUpCmdProperty);

    private static void RowMouseUpCmd_ChangesCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        DataGridRow dataGridRow = (DataGridRow)d;
        dataGridRow.MouseUp += DataGridRow_MouseUp;
    }

    private static void DataGridRow_MouseUp(object sender, MouseButtonEventArgs e)
    {
        DataGridRow row = (DataGridRow)sender;
        ICommand command = GetRowMouseUpCmd(row);
        if (command != null)
            command.Execute(row);
    }
}

DataGrid的row style中绑定命令:

<DataGrid AutoGenerateColumns="False"
            IsReadOnly="True"
            ItemsSource="{Binding TestDataList, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
    <DataGrid.RowStyle>
        <Style TargetType="{x:Type DataGridRow}" BasedOn="{StaticResource DataGridRowStyle}">
            <Setter Property="local:DataGridExtensions.RowMouseUpCmd"
                    Value="{Binding DataContext.DataGridRowMouseUpCmd, RelativeSource={RelativeSource AncestorType=Window}}" />
        </Style>
    </DataGrid.RowStyle>
    <DataGrid.Columns>
        <DataGridTextColumn Header="标题"
                            Binding="{Binding Title}"
                            Width="*" />
        <DataGridTextColumn Header="描述"
                            Binding="{Binding Description}"
                            Width="*" />
    </DataGrid.Columns>
</DataGrid>

其中RelativeSource={RelativeSource AncestorType=Window}表示获取的DataContextWindows控件的DataContext

ViewModel中绑定Command:

public class MainWindowViewModel : BaseViewModel
{
    public ObservableCollection<TestDataGrid> TestDataList { get; set; } = new ObservableCollection<TestDataGrid>();

    public MainWindowViewModel()
    {
        for (int i = 0; i < 10; i++)
        {
            TestDataGrid grid = new TestDataGrid() { Title = $"测试 : {i:0.0}", Description = $"描述 : {i:0.00}" };
            TestDataList.Add(grid);
        }
    }

    public DelegateCommand<DataGridRow> DataGridRowMouseUpCmd => new DelegateCommand<DataGridRow>((row) =>
    {

    });
}

public class TestDataGrid : BindableBase
{
    public string? Title { get; set; }

    public string? Description { get; set; }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值