Silverlight DataGrid 垂直滚动条问题

发布于 2024-10-01 07:12:39 字数 397 浏览 3 评论 0原文

我有一个充满表格的数据网格。现在会出现垂直滚动条,因为表格不适合。到目前为止还好。现在,在最后一列中,我在 xaml 文件中定义了一个按钮。所有这些按钮都有相同的回调,但我可以从表的 selectedIndex 中区分出这个回调应该做什么。因为单击该按钮还会自动选择 DataGrid 中该按钮所在的行。到目前为止还好。现在在我的应用程序中,对于某些行,我想禁用按钮,因为它对该特定行没有意义。所以我所做的是订阅每个按钮的 Load 事件,并让回调设置 MaxWidth = 0(如果按钮没有意义)。这也很好用,但只是最初。一旦我开始拖动滚动条,按钮列中的随机位置就会显示按钮,或者错误的按钮会获得 MaxWidth = 0。我有一种强烈的感觉,在顶部滚动的单元格正在底部被重用,但我没有收到活动,或者至少我不知道应该订阅哪个活动。我不知道如何识别滚动条。有人建议解决这个问题吗?

I have a Datagrid filled with a table. Now the vertical scrollbar shows up because the table doesn't fit. That's fine so far. Now in the last column I have defined a Button in the xaml file. All these buttons have the same callback, but I can distinguish from the selectedIndex of the table what this callback should do. Because clicking the button automatically also selects the line in the DataGrid where this button lives. That's fine so far. Now in my app, for some rows I want to disable the Button, because it has no meaning for that specific row. So what I did is take a subscription on event Load of each Button and let the callback set the MaxWidth = 0, if the button has no meaning. This works fine too, but only initially. As soon as I start dragging the scrollbar, at random places in the Button column buttons show up, or wrong buttons get MaxWidth = 0. I have the strong feeling that cells that scrolled out at the top are being reused at the bottom, but I don't get an event, or at least I don't know which event I should subscribe on. I don't know how to identify the scrollbar. Has anyone a suggestion to tackle this problem?

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

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

发布评论

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

评论(1

滴情不沾 2024-10-08 07:12:39

我自己终于找到了解决这个问题的方法,贴出来记录一下。
您应该订阅的事件是LoadingRow(由DataGrid 生成)。
在回调中,

void TableLoadingRow(object sender, DataGridRowEventArgs e)

您可以使用 VisualTreeHelper 来识别单元格中的元素,如下所示:

private void ButtonSetMaxWidth(DependencyObject reference, int maxWidth)
{
    if (reference != null)
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(reference); i++)
        {
            var child = VisualTreeHelper.GetChild(reference, i);
            if (child.GetType() == typeof(Button))
            {
                Button b = (Button)child;
                if (b.Name == "TheNameOfTheButtonInTheXAML") 
                {
                    b.MaxWidth = maxWidth;
                    return;
                }
            }
            ButtonSetMaxWidth(child, maxWidth);
        }
    }
    return;
}

I finally found a solution to this problem myself, and I will post it for the record.
The event you should subscribe on is LoadingRow (generated by the DataGrid).
In the callback

void TableLoadingRow(object sender, DataGridRowEventArgs e)

you can identify an element in a cell by using VisualTreeHelper for instance as follows:

private void ButtonSetMaxWidth(DependencyObject reference, int maxWidth)
{
    if (reference != null)
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(reference); i++)
        {
            var child = VisualTreeHelper.GetChild(reference, i);
            if (child.GetType() == typeof(Button))
            {
                Button b = (Button)child;
                if (b.Name == "TheNameOfTheButtonInTheXAML") 
                {
                    b.MaxWidth = maxWidth;
                    return;
                }
            }
            ButtonSetMaxWidth(child, maxWidth);
        }
    }
    return;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文