在 DataGrid 上使用 ScrollIntoView 和 Checkbox 会更改行为

发布于 2024-12-12 01:00:23 字数 998 浏览 0 评论 0原文

我似乎有相互矛盾的要求。我有一个 DataGrid,其第一列有一个复选框。用户希望通过单击而不是双击来选择复选框。我能够通过使用 DataGridTemplateColumn 和这样的复选框来实现这一点:

<DataGridTemplateColumn>
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <CheckBox  IsChecked="{Binding IsSelected, UpdateSourceTrigger=PropertyChanged}" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

用户也是一个允许他们指定行(可以有数百行)的控件。如果他们指定不在视图中的行,我希望它滚动到视图中。我妥协并在 DataGrid_SelectionChanged 事件后面的代码中添加了一个事件处理程序。最初我只是使用 ScrollIntoView 命令,但屏幕外的行会突出显示,但网格不会将它们滚动到视图中。然后我可以添加焦点命令并将该行滚动到视图中。所以现在事件处理程序看起来像这样:

private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
   DataGrid dg = (DataGrid)sender;
   if (dg.SelectedItem == null) return;
   dg.ScrollIntoView(dg.SelectedItem);
   dg.SelectedItem.Focus();
}

现在我回到原来的问题,该行滚动到视图中,但要选中任何其他行上的复选框(您不会通过跳转到行控件进入)单击两次。有人知道是什么导致行移动到手动需要双击吗?

I seem to have conflicting requirements. I have a DataGrid that has a checkbox as the first column. The users want the checkbox to be selectable with a single click, not a double click. I was able to make that happen by using a DataGridTemplateColumn and a checkbox like this:

<DataGridTemplateColumn>
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <CheckBox  IsChecked="{Binding IsSelected, UpdateSourceTrigger=PropertyChanged}" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

The user also a control that allows them to specify a row (there can be hundreds of rows). If they specify a row that isn't in view I want it to scroll into view. I compromised and added an event handler in the code behind for the DataGrid_SelectionChanged event. Originally I was just using the ScrollIntoView command but offscreen rows would get highlighted but the grid did not scroll them into view. I was then able to add a Focus command and the row scrolled into view. So now the event handler looks like this:

private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
   DataGrid dg = (DataGrid)sender;
   if (dg.SelectedItem == null) return;
   dg.ScrollIntoView(dg.SelectedItem);
   dg.SelectedItem.Focus();
}

Now I'm back to the original problem, the row scrolls into view but to check the checkbox on any other row (that you don't move into via the jump to row control) you have to click twice. Anybody know what is causing the rows moved to manually to require double clicks?

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

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

发布评论

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

评论(1

笨笨の傻瓜 2024-12-19 01:00:23

好吧,我让它与以下代码一起工作,该代码的灵感来自于一些关于将焦点转移到单元格上的相关帖子。我不知道为什么 ScrollIntoView 不起作用、起作用或者为什么执行最后三行是我可以在不禁用复选框的情况下使行滚动到视图中的一种方法。

private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    DataGrid dg = (DataGrid)sender;
    if (dg.SelectedItem == null) return;
    dg.ScrollIntoView(dg.SelectedItem);

    DataGridRow dg_row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromItem(dg.SelectedItem);
    if (dg_row == null) return;
    dg_row.Focus();
    }

Well I got it to work with the following code which was inspired by some tangentially related posts on getting the focus into cells. I have no clue as to why ScrollIntoView doesn't work, working or why performing the last three lines was the one way I could get the row to scroll into view without disabling the checkbox.

private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    DataGrid dg = (DataGrid)sender;
    if (dg.SelectedItem == null) return;
    dg.ScrollIntoView(dg.SelectedItem);

    DataGridRow dg_row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromItem(dg.SelectedItem);
    if (dg_row == null) return;
    dg_row.Focus();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文