在 WPF 中,访问 ListBox 中的容器
我正在创建一个 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.
}
当清除 DerivedListBox
的 ItemsSource
(或删除项目源中的项目)时,就会出现问题。如何确定 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 DerivedHeaderedContentControl
s, 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于这种特殊情况,您可能应该使用 <代码>ItemsContainerGenerator.ItemsChanged 事件。
For this particular scenario, you should probably use
ItemsContainerGenerator.ItemsChanged
event.