DataGridCheckBoxColumn 工作不正常

发布于 2024-12-15 12:18:38 字数 673 浏览 1 评论 0原文

我刚刚创建了一个 DataGrid:

    <DataGrid>
        <DataGrid.Columns>
            <DataGridTextColumn ... />
            <DataGridCheckBoxColumn Binding="{Binding Path=IsEditing, Mode=TwoWay}" Header="IsEditing" />
        </DataGrid.Columns>
    </DataGrid>

我的问题是当我想首先选中记录内的此复选框时,该行被选中,然后我才能选中此复选框...这是非常不舒服的...此外...当我选择另一行时,创建此 DataGrid 的对象的依赖属性事件也会运行

为了更清楚地说明,我将逐步向您解释:

  1. 我想选择将运行依赖属性事件的项目。
  2. 我单击复选框,但仅选择了已检查的行
  3. 我必须再次单击复选框来检查它,但事件也没有运行
  4. 我必须单击/选择其他行才能运行最后选定的对象/行的依赖属性的事件

为什么这工作这么糟糕吗?有什么选项可以让它对用户(对我来说也是)更舒适地工作吗?

I just created a DataGrid:

    <DataGrid>
        <DataGrid.Columns>
            <DataGridTextColumn ... />
            <DataGridCheckBoxColumn Binding="{Binding Path=IsEditing, Mode=TwoWay}" Header="IsEditing" />
        </DataGrid.Columns>
    </DataGrid>

My problem is that when I want to check this checkbox inside record at first the row is selected and only then I can check this checkbox... It is Very Uncomfortable... And in addition... event for dependency property of objects for which is created this DataGrid also runs when I will select another row.

To be more clearly I will explain you step by step:

  1. I want to select item which will run event of dependency property.
  2. I click on checkbox but insted of cheched only row is selected
  3. I must click AGAIN on checbox to check it, but event also doesn't run
  4. I must click/select other row to make run event of dependency property of last selected object/row

Why this is working so terrible? Is there any option to make it working more comfortable for user(for me also)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你げ笑在眉眼 2024-12-22 12:18:38

您需要为 DataGrid 添加 MouseLeftButtonUp 事件:

    private void GridMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        var grid = sender as DataGrid;

        if (grid == null)
        {
            return;
        }

        // Assume first column is the checkbox column.
        if (grid.CurrentColumn == grid.Columns[0])
        {
            var gridCheckBox = (grid.CurrentColumn.GetCellContent(grid.SelectedItem) as CheckBox);

            if (gridCheckBox != null)
            {
                gridCheckBox.IsChecked = !gridCheckBox.IsChecked;
            }
        }
    }

You will need to add a MouseLeftButtonUp event for the DataGrid:

    private void GridMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        var grid = sender as DataGrid;

        if (grid == null)
        {
            return;
        }

        // Assume first column is the checkbox column.
        if (grid.CurrentColumn == grid.Columns[0])
        {
            var gridCheckBox = (grid.CurrentColumn.GetCellContent(grid.SelectedItem) as CheckBox);

            if (gridCheckBox != null)
            {
                gridCheckBox.IsChecked = !gridCheckBox.IsChecked;
            }
        }
    }
五里雾 2024-12-22 12:18:38

将属性 IsThreeState = "False" 添加到 DataGridCheckBoxColumn

Add Property IsThreeState = "False" to the DataGridCheckBoxColumn

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文