【发布时间】:2016-07-28 11:18:09
【问题描述】:
我很难理解 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# 代码将所有内容加载到 ListBox 中
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);
}
}
【问题讨论】:
-
见this answer。在 WPF 中,您必须使用绑定,这样做
Items.Add()-way 已过时。那Thread.Sleep(1)很臭。您可以使用async/await和await Task.Delay()来避免 UI 停滞。