【发布时间】:2013-12-21 00:00:38
【问题描述】:
在我的组合框中,我添加了 ItemsSource,它是 SelectedItem 的集合。加载屏幕时如何在组合框中默认显示任何项目?
【问题讨论】:
-
将
SelectedItem绑定到ItemsSource中的对象之一。不工作吗?
在我的组合框中,我添加了 ItemsSource,它是 SelectedItem 的集合。加载屏幕时如何在组合框中默认显示任何项目?
【问题讨论】:
SelectedItem 绑定到ItemsSource 中的对象之一。不工作吗?
您是否正在寻找一种默认选择第一项的方法?如果是这样,请尝试以下代码:
<ComboBox SelectedIndex="0">
否则,您应该创建一个属性来存储当前选定的项目并绑定到它:
<ComboBox ItemsSource="{Binding Items}" SelectedItem="{Binding CurrentlySelectedItem}">
【讨论】:
如果您使用的是绑定,即
<ComboBox ItemsSource="{Binding SlowLoadingCollection}"/>
然后您可以添加一个FallbackValue,当无法访问该集合时将使用该FallbackValue。
<ComboBox ItemsSource="{Binding SlowLoadingCollection, FallbackValue='Please wait'}"/>
你可以用类似的东西来测试这个
DataContext.SlowLoadingCollection = null; // No collection, so will display fallback
this.OnLoaded += () =>
{
Task.Run(()=>
{
Task.Delay(10000); // 10 second delay to simulate loading
DataContext.SlowLoadingCollection = new []{ "Hello", "World", "!"};
}
}
【讨论】: