0

私は c#/xaml メトロ アプリケーションを開発しています。

私はリストボックスを持っています.1つの特定の条件に応じて、リストボックスアイテムの背景色を1つだけ変更したいです。

これは私が試した方法です:-

if (lststudents != null)
{
    foreach (StudentClass obj in lststudents)
    {
        if (obj.stutype == "Red House")
        {
///I am unable to typecast to listboxitem, since it is of type student class 
ListBoxItem lstbxbitem = (obj)ListBoxItem;
///
            lstbxbitem.Background = new SolidColorBrush(Colors.Red);
        }
    }
}
lstbxbStudents.ItemsSource = lststudents ;

注意してください:-特定の条件に応じて、一部のリストボックス項目のみの背景色を変更したい。

どうすればこれを達成できるか教えてください??

さまざまな代替手段は何ですか??

前もって感謝します。

4

1 に答える 1

2

ListBox で DataTemplate を使用すると、DataTemplateSelector を作成して、ListBox 内の特定の項目のスタイルを設定できます。

次のリンクを参照できます。

ItemsControl.ItemTemplate プロパティ ItemsControl.ItemTemplateSelector プロパティ

ここに良い記事があります:

Windows 8 ストア アプリで動的 XAML を使用する

于 2013-03-20T08:50:59.843 に答える