Monday, 19 August 2013

ListBox in WPF while using ItemContainerGenerator returns null

ListBox in WPF while using ItemContainerGenerator returns null

I have listbox in my XAML, few checkbox and filter button. My application
generates loads of logs which I display in the listbox. When I have data
in the listbox, what this XAML will do is user will check\uncheck
checkbox. Based on that the data in the listbox will be filtered when
button is clicked. Depending on data i want to show different forecolor
and background color on each item.
private void FilterButton_Click ( object sender , RoutedEventArgs e )
{
//Whenever filter button is clicked, i will check for checkbox status.
whichever //checkbox is ON I will add checkbox name into Dictionary.
Then I will read string from Listbox and extract particular keyword
from that and match with Dictionary key. If it //matches then I will
modify the background and foreground color for that particualr
//listbox items. My problem here is only certain Listbox items get
updated rest of them are //unchaged. When debugged i found that
itemcontainergenerator returns null for all other //items.
for ( int i = 0 ; i < ListBox1.Items.Count ; i++ )
{
ListBoxItem item1 = ( ListBoxItem
)ListBox1.ItemContainerGenerator.ContainerFromIndex(i);
string recordType;
string [] contentArray;
if ( item1 == null )
continue;
if ( item1.Content == "" )
continue;
contentArray = item1.Content.ToString().Split( new char [] { ','
}, StringSplitOptions.RemoveEmptyEntries );
recordType = contentArray [ 1 ];
if ( checkBoxType.ContainsKey ( recordType ))
{
//int code = RecordTypeToColorCode [ recordType ];
//item1.Foreground = ColorCodeToForeColor [ code ];
item1.Foreground = Brushes.DarkCyan;
item1.FontSize = 13;
item1.Background = Brushes.LightGoldenrodYellow;
}
else
{
item1.Foreground = Brushes.LightGray;
}
}
}
The issue I am seeing is that if suppose my listbox has 1000 items then
only 35-40 items are updated. Rest all items are same. I debugged more
into the code and I found that after some number 35-40 all items are
coming as null which i why i am not able to update all items in the
listbox. I have not turned on Virtualization in my code. Is there any way
I can update all the items. Any help is appreciated. I am thinking if
there is any issue with ItemCOntainerGenerator, as it only displays
certain items, also Virtualization is turned off. I am newbie to WPF so
please bare with me if its silly question.

No comments:

Post a Comment