我很难理解 XAML 如何与 C# 结合使用。
我的问题是,我有两个不同的List<String>
充满内容的物体,我想要一个List<String>
一个的背景颜色为“蓝色”,另一个的背景颜色为“红色”。之后我想将其显示在我的ListBox
我的 XAML 列表框代码:
<ListBox x:Name="ListBox1" HorizontalAlignment="Left" Height="240" Margin="81,80,0,0" VerticalAlignment="Top" Width="321" BorderBrush="#FF6C6C6C" SelectionMode="Single" SelectionChanged="ListBoxSelectionChanged">
</ListBox>
我的 C# 代码将所有内容加载到列表框中
public void AddItemsToListBox()
{
foreach (var object1 in objects1)
{
//I want these Objects to be blue
listBox1.Items.Add(object1.label);
}
foreach (var object2 in objects2)
{
//I want these Objects to be red
listBox1.Items.Add(object2.label);
}
}
干得好:
foreach (var object1 in objects1)
{
Thread.Sleep(1);
listBox1.Items.Add(new ListBoxItem { Content = object1.label, Background = Brushes.Blue });
}
foreach (var object2 in objects2)
{
Thread.Sleep(1);
ListBox2.Items.Add(new ListBoxItem { Content = objects2.label, Background = Brushes.Red });
//I want these Objects to be red
}
更好的方法是使用数据绑定、样式等。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)