Note that there are some explanatory texts on larger screens.

plurals
  1. POListView.ItemContainerGenerator.ContainerFromItem(item) return null after 20 items
    primarykey
    data
    text
    <p>first of all I want to explain what I'm trying to do. I have a <code>ListView</code> in a <code>UserControl</code> with a <code>DataTemplate</code> defined as a resource. I want to hide a button inside the DataTemplate. Sounds easy, but ....</p> <p>The code I'm using is </p> <pre><code>&lt;UserControl.Resources&gt; &lt;DataTemplate x:Key="Proyectos"&gt; &lt;DockPanel Name="Panel" Margin="0,0,0,0" MinWidth="1200" MaxWidth="1200"&gt; &lt;Border Margin="0" BorderBrush="Bisque" BorderThickness="1" DockPanel.Dock="Left"&gt; &lt;StackPanel Margin="0" Width="1135"&gt; &lt;DockPanel&gt; &lt;TextBlock Text="{Binding titulo}" Name="titulo" FontWeight="Bold" FontSize="12" /&gt; &lt;/DockPanel&gt; &lt;DockPanel &gt; &lt;TextBlock FontWeight="Bold" Text="Nº Ref. Fundacion: " DockPanel.Dock="Left" Margin="5,0,5,0" FontSize="11"/&gt; &lt;TextBlock Name="txb_codproy" Text="{Binding codproy}" FontSize="11"/&gt; &lt;TextBlock FontWeight="Bold" Text=" Nº Ref. Proyecto: " FontSize="11"/&gt; &lt;TextBlock Text="{Binding registro}" FontSize="11"/&gt; &lt;TextBlock FontWeight="Bold" Text=" Estado: " FontSize="11"/&gt; &lt;TextBlock Text="{Binding estados_proyecto.descripcion}" FontSize="11"/&gt; &lt;/DockPanel&gt; &lt;DockPanel &gt; &lt;TextBlock FontWeight="Bold" Text="Organismo " DockPanel.Dock="Left" Margin="5,0,5,0" FontSize="11"/&gt; &lt;TextBlock Text="{Binding organismo.descripcion}" FontSize="11"/&gt; &lt;/DockPanel&gt; &lt;/StackPanel&gt; &lt;/Border&gt; &lt;Border Margin="0" Width="Auto" BorderBrush="Transparent" BorderThickness="1" Background="White" HorizontalAlignment="Left"&gt; &lt;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"&gt; &lt;Image Name="img_eliminar" Width="48" Source="imagenes/borrar.png" Height="19" /&gt; &lt;/Button&gt; &lt;/Border&gt; &lt;/DockPanel&gt; &lt;/DataTemplate&gt; &lt;/UserControl.Resources&gt; &lt;Grid Width="1300" Height="845.169"&gt; &lt;ListView Margin="20,120.024,15.247,50" MouseDoubleClick="list_proyectos_MouseDoubleClick" Name="list_proyectos" ItemsSource="{Binding}" ItemTemplate="{StaticResource Proyectos}"&gt; &lt;/ListView&gt; &lt;TextBox Margin="32,12,35,0" Name="txt_busqueda" TextChanged="textBox1_TextChanged" Background="AliceBlue" BorderBrush="Gray" Height="23" VerticalAlignment="Top" /&gt; &lt;/Grid&gt; ////////////////////////////////////////////////////////// public Proyectos(IPrincipal identityA) { list_proyectos.ItemsSource = ListaProyectos; 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_StatusChanged; 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&lt;ContentPresenter&gt;(lbitem); DataTemplate myDataTemplate = contentPresenter.ContentTemplate; Button b = (Button)lbitem.ContentTemplate.FindName("btn_Eliminar", contentPresenter); b.Visibility = Visibility.Hidden; } } } private T FindVisualChild&lt;T&gt;(DependencyObject obj) where T : DependencyObject { for (int i = 0; i &lt; VisualTreeHelper.GetChildrenCount(obj); i++) { DependencyObject child = VisualTreeHelper.GetChild(obj, i); if (child != null &amp;&amp; child is T) return (T)child; } for (int i = 0; i &lt; VisualTreeHelper.GetChildrenCount(obj); i++) { DependencyObject child = VisualTreeHelper.GetChild(obj, i); T childOfChild = FindVisualChild&lt;T&gt;(child); if (childOfChild != null) return childOfChild; } return null; } </code></pre> <p>I found two problems with this, </p> <ol> <li><p>This line <code>(ListBoxItem)list_proyectos.ItemContainerGenerator.ContainerFromItem(item);</code> returns null after the 16th item. The listview has 1576 items</p></li> <li><p>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.</p></li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload