在 WPF 中,访问 ListBox 中的容器

发布于 2024-08-08 19:47:28 字数 1179 浏览 1 评论 0原文

我正在创建一个 DerivedListBox : ListBox 和一个 DerivedHeaderedContentControl : HeaderedContentControl,它们将用作 ListBox 中每个项目的容器。

为了计算可用于 DerivedHeaderedContentControl 的扩展内容的大小,我将每个容器对象存储在 DerivedListBox 内的列表中。这样我就可以计算每个 DerivedHeaderedContentControl 的标题高度,并从 DerivedListBox 可用的总大小中减去该高度。这将是可用于 DerivedHeaderedContentControl 的扩展内容的大小。

public class DerivedHeaderedContentControl : HeaderedContentControl
{
    // Do some binding to DerivedListBox to calculate height.
}

public class DerivedListBox : ListBox
{
    private List<DerivedHeaderedContentControl> containers;

    protected override DependencyObject GetContainerForItemOverride()
    {
        DerivedHeaderedContentControl val = new DerivedHeaderedContentControl();
        this.containers.Add(val);
        return val;
    }

    // Do some binding to calculate height available for an expanded
    // container by iterating over containers.
}

当清除 DerivedListBoxItemsSource(或删除项目源中的项目)时,就会出现问题。如何确定 ItemsSource 何时被清除,以便我可以清除容器列表?

I'm creating a DerivedListBox : ListBox and a DerivedHeaderedContentControl : HeaderedContentControl, which will serve as a container for each item in the ListBox.

In order to calculate the size available for the expanded content of the DerivedHeaderedContentControls, I am storing each container object in a list within the DerivedListBox. This way I can calculate the height of the headers of each DerivedHeaderedContentControl and subtract that from the total size available to the DerivedListBox. This would be the size available for the expanded content of a DerivedHeaderedContentControl.

public class DerivedHeaderedContentControl : HeaderedContentControl
{
    // Do some binding to DerivedListBox to calculate height.
}

public class DerivedListBox : ListBox
{
    private List<DerivedHeaderedContentControl> containers;

    protected override DependencyObject GetContainerForItemOverride()
    {
        DerivedHeaderedContentControl val = new DerivedHeaderedContentControl();
        this.containers.Add(val);
        return val;
    }

    // Do some binding to calculate height available for an expanded
    // container by iterating over containers.
}

The problem comes in when the DerivedListBox's ItemsSource is cleared (or an item in the items source is removed). How can I determine when the ItemsSource is cleared so that I can clear the containers list?

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

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

发布评论

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

评论(1

颜漓半夏 2024-08-15 19:47:28

对于这种特殊情况,您可能应该使用 <代码>ItemsContainerGenerator.ItemsChanged 事件。

For this particular scenario, you should probably use ItemsContainerGenerator.ItemsChanged event.

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