XAML にリストボックスがあり、いくつかのチェックボックスとフィルター ボタンがあります。私のアプリケーションは、リストボックスに表示する大量のログを生成します。リストボックスにデータがある場合、この XAML が行うことは、ユーザーがチェックボックスをオン/オフにすることです。それに基づいて、ボタンがクリックされたときにリストボックス内のデータがフィルタリングされます。データに応じて、各アイテムに異なる前色と背景色を表示したいと考えています。
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;
}
}
}
私が見ている問題は、リストボックスに 1000 個のアイテムがあると仮定すると、35 ~ 40 個のアイテムしか更新されないことです。その他の項目はすべて同じです。コードをさらにデバッグしたところ、35 から 40 の数の後、すべての項目が null になっていることがわかりました。これが、リストボックス内のすべての項目を更新できない理由です。コードで仮想化を有効にしていません。すべてのアイテムを更新する方法はありますか。どんな助けでも大歓迎です。特定のアイテムのみを表示し、仮想化もオフになっているため、ItemCONtainerGenerator に問題があるかどうかを考えています。