1

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 に問題があるかどうかを考えています。

4

2 に答える 2

0

そのようにしてはいけません。代わりに、アイテムが実際にフィルター処理されているかどうかを制御するビュー モデル アイテム クラスにブール値のプロパティがある場合があります (それを と呼びましょうIsFiltered)。ItemContainerStyle次に、プロパティの値によって影響を受けるすべてのプロパティを設定する ListBoxの に DataTrigger を追加しIsFilteredます。もちろん、そのプロパティの INotifyPropertyChanged も実装する必要があります。

<ListBox ...>
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="Foreground" Value="LightGray"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsFiltered}" Value="True">
                    <Setter Property="Foreground" Value="DarkCyan"/>
                    <Setter Property="Background" Value="LightGoldenrodYellow"/>
                    <Setter Property="FontSize" Value="13"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ListBox.ItemContainerStyle>
    ...
</ListBox>

フィルター条件が変更されるたびに、アイテム コレクション (コンテナーではなく) を反復処理し、IsFiltered各アイテムのプロパティに値を設定します。

于 2013-08-20T07:46:04.037 に答える