gpt4 book ai didi

wpf - 在 Codebehind WPF 中隐藏具有数据模板的列表框的项目

转载 作者:行者123 更新时间:2023-12-04 16:13:06 24 4
gpt4 key购买 nike

我有一个列表框,其中 DataTemplate 分配给它的 ItemTemplate,如下所示:

<ListBox Name="DayOverview">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Width="125" Background="Transparent" Orientation="Horizontal" MouseLeftButtonDown="DayOverview_MouseLeftButtonDown">
<Label Content="{Binding Owner}"/>
<Label Content="{Binding Subject}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

我想将它的一些项目可见性设置为在代码隐藏中折叠,如下所示:

(DayOverview.Items[i] as ListBoxItem).Visibility = Visibility.Collapsed;

但我无法将 Its 的项目转换为 ListBoxItem 或任何其他控件,项目是类的类型。是否可以在 C# 中使用数据模板隐藏列表框的项目?

最佳答案

DayOverview.Items 集合将包含您将 ItemsSource 属性绑定(bind)到的项目。它是数据而不是视觉容器。 ListBox 将使用 ItemContainerGenerator 为您的每个项目生成一个 ListBoxItem 容器。

您可以使用 ItemContainerGenerator 的 ContainerFromIndex 方法从数据项中获取可视容器。

试试这个:

DayOverview.ItemContainerGenerator.ContainerFromIndex(i) as ListBoxItem).Visibility = Visibility.Collapsed;

关于wpf - 在 Codebehind WPF 中隐藏具有数据模板的列表框的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25157000/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com