[英]Displaying Specific values in WPF Combobox
In my combobox i have added ItemsSource which is a collection of SelectedItem. 在我的组合框中,我添加了ItemsSource,它是SelectedItem的集合。 How to display any item by default in the combobox while loading the screen?
如何在加载屏幕时在组合框中默认显示任何项目?
Are you looking for a way to select first item by default? 您是否正在寻找默认选择第一项的方法? If so, try this code:
如果是这样,请尝试以下代码:
<ComboBox SelectedIndex="0">
Otherwise, you should create a property to store currently selected item and bind to it: 否则,您应该创建一个属性来存储当前选定的项并绑定到它:
<ComboBox ItemsSource="{Binding Items}" SelectedItem="{Binding CurrentlySelectedItem}">
If you are using a binding ie 如果你正在使用绑定即
<ComboBox ItemsSource="{Binding SlowLoadingCollection}"/>
then you can add a FallbackValue
that will be used when the collection cannot be accessed. 然后,您可以添加将在无法访问集合时使用的
FallbackValue
。
<ComboBox ItemsSource="{Binding SlowLoadingCollection, FallbackValue='Please wait'}"/>
you can test this with something like 你可以用类似的东西测试一下
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", "!"};
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.