C# 在WPF组合框中显示特定值
在我的组合框中,我添加了ItemsSource,它是SelectedItem的集合。加载屏幕时,如何在组合框中默认显示任何项目 您是否在寻找默认情况下选择第一项的方法?如果是,请尝试以下代码:C# 在WPF组合框中显示特定值,c#,wpf,C#,Wpf,在我的组合框中,我添加了ItemsSource,它是SelectedItem的集合。加载屏幕时,如何在组合框中默认显示任何项目 您是否在寻找默认情况下选择第一项的方法?如果是,请尝试以下代码: <ComboBox SelectedIndex="0"> 否则,应创建一个属性来存储当前选定的项并绑定到该项: <ComboBox ItemsSource="{Binding Items}" SelectedItem="{Binding CurrentlySelectedItem}
<ComboBox SelectedIndex="0">
否则,应创建一个属性来存储当前选定的项并绑定到该项:
<ComboBox ItemsSource="{Binding Items}" SelectedItem="{Binding CurrentlySelectedItem}">
如果您使用的是绑定,即
<ComboBox ItemsSource="{Binding SlowLoadingCollection}"/>
你可以用这样的方法来测试
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", "!"};
}
}
将
SelectedItem
绑定到ItemsSource
中的一个对象。这不管用吗?我只添加了这样的内容。“Items”是我的可观察集合,它第一次设置值并获取所选的项名称,但不知道为什么第二次命中set属性时该值变为null。因此,在加载页面时,该值不是通过DEAFULT在combobox中选择的。
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", "!"};
}
}