ItemContainerGenerator.ContainerFromItem() returns null while VirtualizingStackPanel.IsVirtualizing=“False”

后端 未结 3 433
清歌不尽
清歌不尽 2021-01-13 13:47

I\'m facing a similar problem with this question however VirtualizingStackPanel.IsVirtualizing=\"False\" didn\'t solve my problem. Is there anyone facing the sa

相关标签:
3条回答
  • 2021-01-13 13:55

    try execute UpdateLayout() before this.ItemContainerGenerator.ContainerFromItem(item)

    0 讨论(0)
  • Use ItemContainerGenerator.StatusChanged event from you ComboBox like this:

    myComboBox.ItemContainerGenerator.StatusChanged += ItemContainerGenerator_StatusChanged;
    
    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);
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-13 14:14

    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.

    0 讨论(0)
提交回复
热议问题