WPF中ItemContainerGenerator的Status变化社会中的政治秩序

没有更多推荐了,
不良信息举报
举报内容:
WPF TreeView的TreeViewItem查找
举报原因:
原文地址:
原因补充:
最多只允许输入30个字
加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!wpf - ListView.ItemContainerGenerator.ContainerFromItem(item) return null after 20 items - Stack Overflow
Stack Overflow for Teams
A private, secure home for your team's questions and answers.
to customize your list.
This site uses cookies to deliver our services and to show you relevant ads and job listings.
By using our site, you acknowledge that you have read and understand our
Your use of Stack Overflow’s Products and Services, including the Stack Overflow Network, is subject to these policies and terms.
first of all I want to explain what I'm trying to do. I have a ListView in a UserControl with a DataTemplate defined as a resource. I want to hide a button inside the DataTemplate. Sounds easy, but ....
The code I'm using is
&UserControl.Resources&
&DataTemplate x:Key="Proyectos"&
&DockPanel Name="Panel"
Margin="0,0,0,0" MinWidth="1200" MaxWidth="1200"&
&Border Margin="0" BorderBrush="Bisque" BorderThickness="1" DockPanel.Dock="Left"&
&StackPanel Margin="0" Width="1135"&
&DockPanel&
&TextBlock
Text="{Binding titulo}" Name="titulo" FontWeight="Bold" FontSize="12" /&
&/DockPanel&
&DockPanel &
&TextBlock FontWeight="Bold" Text="N? Ref. Fundacion: " DockPanel.Dock="Left" Margin="5,0,5,0" FontSize="11"/&
&TextBlock Name="txb_codproy"
Text="{Binding codproy}" FontSize="11"/&
&TextBlock FontWeight="Bold" Text="
N? Ref. Proyecto: " FontSize="11"/&
&TextBlock Text="{Binding registro}" FontSize="11"/&
&TextBlock FontWeight="Bold" Text="
Estado: " FontSize="11"/&
&TextBlock Text="{Binding estados_proyecto.descripcion}" FontSize="11"/&
&/DockPanel&
&DockPanel &
&TextBlock FontWeight="Bold" Text="Organismo " DockPanel.Dock="Left" Margin="5,0,5,0" FontSize="11"/&
&TextBlock Text="{Binding organismo.descripcion}" FontSize="11"/&
&/DockPanel&
&/StackPanel&
&Border Margin="0" Width="Auto" BorderBrush="Transparent" BorderThickness="1" Background="White" HorizontalAlignment="Left"&
&Button Margin="0,0,0,0" VerticalAlignment="Center" HorizontalAlignment="Left" Name="btn_Eliminar" Click="btn_Eliminar_Click" Width="Auto" Height="25" Background="Transparent" BorderBrush="Transparent"&
&Image Name="img_eliminar" Width="48" Source="imagenes/borrar.png" Height="19" /&
&/DockPanel&
&/DataTemplate&
&/UserControl.Resources&
&Grid Width="1300" Height="845.169"&
&ListView Margin="20,120.024,15.247,50" MouseDoubleClick="list_proyectos_MouseDoubleClick"
Name="list_proyectos" ItemsSource="{Binding}" ItemTemplate="{StaticResource Proyectos}"&
&/ListView&
&TextBox Margin="32,12,35,0" Name="txt_busqueda" TextChanged="textBox1_TextChanged" Background="AliceBlue" BorderBrush="Gray" Height="23" VerticalAlignment="Top" /&
//////////////////////////////////////////////////////////
public Proyectos(IPrincipal identityA)
list_proyectos.ItemsSource = ListaP
list_proyectos.ItemContainerGenerator.StatusChanged += new EventHandler(ItemContainerGenerator_StatusChanged);
void ItemContainerGenerator_StatusChanged(object sender, EventArgs e)
if (list_proyectos.ItemContainerGenerator.Status == System.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated)
list_proyectos.ItemContainerGenerator.StatusChanged -= ItemContainerGenerator_StatusC
Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Input, new VoidDelegate(DelayedAction));
delegate void VoidDelegate();
void DelayedAction()
foreach (object item in list_proyectos.Items)
ListBoxItem lbitem = (ListBoxItem)list_proyectos.ItemContainerGenerator.ContainerFromItem(item);
if (lbitem != null)
ContentPresenter contentPresenter = FindVisualChild&ContentPresenter&(lbitem);
DataTemplate myDataTemplate = contentPresenter.ContentT
Button b = (Button)lbitem.ContentTemplate.FindName("btn_Eliminar", contentPresenter);
b.Visibility = Visibility.H
private T FindVisualChild&T&(DependencyObject obj)
where T : DependencyObject
for (int i = 0; i & VisualTreeHelper.GetChildrenCount(obj); i++)
DependencyObject child = VisualTreeHelper.GetChild(obj, i);
if (child != null && child is T)
return (T)
for (int i = 0; i & VisualTreeHelper.GetChildrenCount(obj); i++)
DependencyObject child = VisualTreeHelper.GetChild(obj, i);
T childOfChild = FindVisualChild&T&(child);
if (childOfChild != null)
return childOfC
I found two problems with this,
This line (ListBoxItem)list_proyectos.ItemContainerGenerator.ContainerFromItem(item); returns null after the 16th item. The listview has 1576 items
When the ListView is shown and the first 16 items have the button hidden, if I scroll down to the end and then go to top again the buttons are visible again.
14.8k33658
The ListView is using virtualization, therefore it will not have created any containers for items that it knows it doesn't have to display. This is a "good thing", especially considering you have 1576 items.
Perhaps you can explain why you want to be able to get the container for the item that is not visible and we can provide better suggestions as far as what you can do.
28.7k26390
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Post as a guest
Post as a guest
By clicking &Post Your Answer&, you acknowledge that you have read our updated ,
and , and that your continued use of the website is subject to these policies.
Not the answer you're looking for?
Browse other questions tagged
Stack Overflow works best with JavaScript enabledc# - ItemContainerGenerator.ContainerFromItem() returns null while VirtualizingStackPanel.IsVirtualizing=&False& - Stack Overflow
Stack Overflow for Teams
A private, secure home for your team's questions and answers.
to customize your list.
This site uses cookies to deliver our services and to show you relevant ads and job listings.
By using our site, you acknowledge that you have read and understand our
Your use of Stack Overflow’s Products and Services, including the Stack Overflow Network, is subject to these policies and terms.
I'm facing a similar problem with
however VirtualizingStackPanel.IsVirtualizing="False" didn't solve my problem. Is there anyone facing the same issue?
The thing is I have a custom combobox,
&Style TargetType="{x:Type MultiSelectionComboBox}"
&Setter Property="VerticalContentAlignment" Value="Center"/&
&Setter Property="ItemsPanel"&
&Setter.Value&
&ItemsPanelTemplate&
&StackPanel Orientation="Vertical"
VerticalAlignment="Center"
HorizontalAlignment="Center"
VirtualizingStackPanel.IsVirtualizing="False"/&
&/ItemsPanelTemplate&
&/Setter.Value&
&Setter Property="ItemTemplate"&
&Setter.Value&
&DataTemplate&
&StackPanel Orientation="Horizontal" x:Name="ItemStack" VirtualizingStackPanel.IsVirtualizing="False"&
&CheckBox x:Name="CheckBoxItem"
Command="{Binding SelectItem, RelativeSource={RelativeSource AncestorType={x:Type MultiSelectionComboBox}}}"
CommandParameter="{Binding Key}"
&/CheckBox&
&TextBlock Text="{Binding DisplayText}"&&/TextBlock&
&/StackPanel&
&/DataTemplate&
&/Setter.Value&
&Setter Property="Template"&
&Setter.Value&
&ControlTemplate TargetType="{x:Type ComboBox}"&
&Grid x:Name="Placement" SnapsToDevicePixels="true"&
&Grid.ColumnDefinitions&
&ColumnDefinition Width="*"/&
&ColumnDefinition Width="Auto"/&
&/Grid.ColumnDefinitions&
&Border BorderThickness="1" BorderBrush="Black"&
&TextBox IsReadOnly="True" Grid.Column="0"
Text="{Binding Text, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource AncestorType={x:Type MultiSelectionComboBox}}}"&
&/TextBox&
&Popup x:Name="PART_Popup"
Grid.Column="0"
Focusable="False"
Grid.ColumnSpan="2"
IsOpen="{Binding IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
Placement="Bottom"
VerticalOffset="-1"
PlacementTarget="{Binding ElementName=LayoutRoot}"&
&Popup.Resources&
&Style TargetType="{x:Type ScrollBar}" BasedOn="{StaticResource {x:Type ScrollBar}}"&
&Style.Triggers&
&Trigger Property="Orientation" Value="Vertical"&
&Setter Property="BorderThickness" Value="0"/&
&/Trigger&
&/Style.Triggers&
&/Popup.Resources&
&ScrollViewer x:Name="DropDownScrollViewer"
Background="{StaticResource Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
MinWidth="{Binding ActualWidth, ElementName=LayoutRoot}"
MaxHeight="{TemplateBinding MaxDropDownHeight}"&
&ItemsPresenter KeyboardNavigation.DirectionalNavigation="Contained"/&
&/ScrollViewer&
&ToggleButton IsEnabled="{Binding IsEnabled, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource AncestorType={x:Type MultiSelectionComboBox}}}" Grid.Column="1" IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" Style="{StaticResource ComboBoxToggleButton}"/&
&/ControlTemplate&
&/Setter.Value&
&/ResourceDictionary&
and yet I can't get a reference to the checkbox inside via,
this.ItemContainerGenerator.ContainerFromItem(this.Items[0]) as ComboBoxI
Is there any suggestions?
What i actually want to achieve is,
i want to change checkboxes ischecked property which is depending on an other object which can change on runtime. I can't do it with using bindings due to the current state of the overall project which i can not change at this point. So basically once the new MultiSelectionComboBox is created i want to do something like this,
foreach (object item in this.Items)
ComboBoxItem comboBoxItem = this.ItemContainerGenerator.ContainerFromItem(item) as ComboBoxI
if (comboBoxItem == null)
FrameworkElement element = comboBoxItem.ContentTemplate.LoadContent() as FrameworkE
CheckBox checkBox = element.FindName("CheckBoxItem") as CheckB
checkBox.IsChecked = this.SelectedItem.Contains(item);
try execute UpdateLayout() before this.ItemContainerGenerator.ContainerFromItem(item)
Use ItemContainerGenerator.StatusChanged event from you ComboBox like this:
myComboBox.ItemContainerGenerator.StatusChanged += ItemContainerGenerator_StatusC
void ItemContainerGenerator_StatusChanged(object sender, System.EventArgs e)
if (myComboBox.ItemContainerGenerator.Status == GeneratorStatus.ContainersGenerated)
foreach (var item in myComboBox.Items)
var container = (ComboBoxItem)LanguageComboBox.ItemContainerGenerator.ContainerFromItem(item);
As my logic was in the SelectionChanged event, i wondered why the ItemContainerGenerator.ContainerFromItem method always returned null even if the Listbox.SelectedItem was not null and even more strange, Virtualisation was turned off! Looking at the ItemContainerGenerator.Status i saw that it was Primitives.GeneratorStatus.NotStarted then i added a simple test on ItemContainerGenerator.Status == Primitives.GeneratorStatus.ContainersGenerated and finally solved it that way and no need to subsribe to the Status_Changed event.
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Post as a guest
Post as a guest
By clicking &Post Your Answer&, you acknowledge that you have read our updated ,
and , and that your continued use of the website is subject to these policies.
Not the answer you're looking for?
Browse other questions tagged
Stack Overflow works best with JavaScript enabled

我要回帖

更多关于 中国2012到2017的变化 的文章

 

随机推荐