none
ListView.ItemContainerGenerator and grouping RRS feed

  • Question

  • Hi,

     

    I'have the same problem that RoboMoose, last week...

    I'm trying to use the ItemContainerGenerator.ContainerFromItem() function to get the container associated with a value in my ListView.


    This works fine when binding the Source of the list view with a CollectionViewSource.

    I can sort and group, it still work ...

    But when I try to set a style on my group, ContainerFromItem() returns null.

     

    Can you help me ?

     

    I'have a small project that I can send you

    Thanks a lot

    Monday, March 31, 2008 1:40 PM

Answers

  •  Nikus73 wrote:

    I think it's a cross thread problem ... With my work around, I wait until the rendering is done and I'm sur the generator find the UIElement ...


    Well, it's rather a timing issue, because only one thread aka UI thread is involved here, your workround should be a reasonable approach, but I'd rather generalize it so that if I already can get meaningful result from ContainerFromItem, I simply manipulate it, if not, BeginInvoke to schedule the operation at the later time.

     Nikus73 wrote:

    Do you think that this "side effect" will be corrected in a next release of framework ?


    I cannot say when this "side effect" will be fixed, I am not working at the product team, but suffice it to say, it's rather a timing issue, and when we encounter such type of problem, BeginInvoke should be the way to go.

    Hope this helps
    Thursday, April 3, 2008 4:08 AM

All replies

  • This is a side effect of the UI virtualization, you can turn off virtualization using the following code:

    <ListView VirtualizingStackPanel.IsVirtualizing="False"/>

    Hope this helps
    Wednesday, April 2, 2008 3:41 AM
  • Thank,
    But it still doesn't work !!!
    ItemContainerGenerator.ContainerFromItem still return null 

    I need Virtualization because I can have more than 5000 items in my ListView if user disable filter ...

    But ...
    If I invoke a delegate in ItemContainerGenerator_StatusChanged event, with a lower priority than Rendering, it work ...
    Something like this :
     

    Code Snippet

    private void Window_Loaded(object sender, RoutedEventArgs e) {

    ...

     

    myListView.ItemContainerGenerator.StatusChanged += new EventHandler(ItemContainerGenerator_StatusChanged);

     

    ...

    }

     

    void ItemContainerGenerator_StatusChanged(object sender, EventArgs e) {

    if (myListView.ItemContainerGenerator.Status == System.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated) {

    Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Input, new SampleDelegate(RefreshStateInList));

    }

    }

     

    delegate void SampleDelegate();

    private void RefreshStateInList() {

    if (myListView.ItemContainerGenerator.Status == System.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated) {

    // Job to do ...

    // In example

    foreach (Item l_Item in m_Items) {

    if (l_Item.State) {

    (myListView.ItemContainerGenerator.ContainerFromItem(l_Item) as ListBoxItem).SetValue(IsItemStatusHighlightedProperty, true);

    }

    }

    }

    }

     

     

    I think it's a cross thread problem ... With my work around, I wait until the rendering is done and I'm sur the generator find the UIElement ...

    But it's just a work around !!!

     

    Do you think that this "side effect" will be corrected in a next release of framework ?

    Wednesday, April 2, 2008 6:56 AM
  •  Nikus73 wrote:

    I think it's a cross thread problem ... With my work around, I wait until the rendering is done and I'm sur the generator find the UIElement ...


    Well, it's rather a timing issue, because only one thread aka UI thread is involved here, your workround should be a reasonable approach, but I'd rather generalize it so that if I already can get meaningful result from ContainerFromItem, I simply manipulate it, if not, BeginInvoke to schedule the operation at the later time.

     Nikus73 wrote:

    Do you think that this "side effect" will be corrected in a next release of framework ?


    I cannot say when this "side effect" will be fixed, I am not working at the product team, but suffice it to say, it's rather a timing issue, and when we encounter such type of problem, BeginInvoke should be the way to go.

    Hope this helps
    Thursday, April 3, 2008 4:08 AM
  • Well the solution is much simpler than anyone has ever thought. Just set the IsExpanded property of the newly inserted node to true and that would take away all the pain :)

     

    Kuntal Kishore

    Tuesday, October 26, 2010 9:05 PM
  • Hello!

    I am facing a problem and I should resolve it by tomorrow since I have an urgent deadline! I really need help!!!

    I am using WPF, I have 2 projects:

    - forms projects

    - client project used to load the available forms in the forms project

    In the forms project, I have a ListBox and I bound it to a DataTemplate containing the UI of vendors information.

    At runtime, the form is loaded in the client project. By default only one vendor is created and there are 2 buttons: expand & collapse to add or remove vendors from the list.

    The implementation on the client side is done using reflection! this means I get the type of the object from the forms and use reflection to access its info. One project could contain vendors info, another one cars info...

    The problem I am getting is that I have a custom control that I have created for attachments. In my datatemplate the attachment control is bound to a list of files but since this is a custom control, I need to handle special conditions on the client side for that control.

    Let's say at runtime, the listbox is filled with 2 vendors...

    I need to access the element with its value from the client project.

    When I loop over the items in my listbox entitled lBox and add the following line:

     ListBoxItem lbi = (ListBoxItem)(lBox.ItemContainerGenerator.ContainerFromItem(lBox.Items[i]));

    I get an error since lbi is always null and the error says it can't return null!!!

    If I try the same code in the forms project, I could access the element and even set its value however this error shows when I access it from the client project!!!

    How can I solve this?

    I tried to read this post and tried the solutions but it didn't work as well! Where should I set the IsExpanded property? Any other solution for that!?

    Plz try to help me with sample code since I have to fix this asap! Thank you very much in advance!



                       

     

     

     

     


    larak
    Thursday, November 25, 2010 6:46 PM
  •  Nikus73 wrote:

    I think it's a cross thread problem ... With my work around, I wait until the rendering is done and I'm sur the generator find the UIElement ...


    Well, it's rather a timing issue, because only one thread aka UI thread is involved here, your workround should be a reasonable approach, but I'd rather generalize it so that if I already can get meaningful result from ContainerFromItem, I simply manipulate it, if not, BeginInvoke to schedule the operation at the later time.

     <//font>Nikus73 wrote:

    <//font>Do you think that this "side effect" will be corrected in a next release of framework ?


    I cannot say when this "side effect" will be fixed, I am not working at the product team, but suffice it to say, it's rather a timing issue, and when we encounter such type of problem, BeginInvoke should be the way to go.

    Hope this helps

    Thanks a lots. I like Microsoft in general. I hate, I  hate then so much for not correcting their bugs quickly when we found them.

    Eric Ouellet

    Monday, November 19, 2012 7:24 PM