WPF comboBox selectedIndex = 0不起作用 [英] wpf comboBox selectedIndex=0 is not working
本文介绍了WPF comboBox selectedIndex = 0不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
.xaml
<ComboBox Grid.Row="0" Grid.Column="1" x:Name="cbx_srchResOrg" HorizontalAlignment="Stretch" Style="{DynamicResource ComboBoxStyle}"
ItemsSource="{Binding InfoCombo}" SelectedIndex="0" DisplayMemberPath="Dis_name" SelectedValuePath="Hide_id" SelectedItem="{Binding SelectInfo}"/>
这是我的源代码的一部分。
为什么 'SelectedIndex = 0' 不起作用?
我想在第一次选择[0]值作为默认值,但是在运行时它只是一个空框。除此之外没有其他错误。我该如何解决?
Here is a part of my source code. Why 'SelectedIndex=0' is not working? I want to select [0] value to default at first time, but it just empty box at run time. There are no errors except it. How can I fix it?
推荐答案
如Hej所说,您已将 SelectedItem
绑定到一个属性在视图模型中为 null
。
As Hej said, you have binded the SelectedItem
with a property in your view model which was null
.
您可以通过分配 SelectedItem来解决此问题
在您的Viewmodel构造函数中
You can fix this by assigning the SelectedItem
in your Viewmodel constructor
Public MyViewModel()
{
SelectInfo = InfoCombo[0];
}
这篇关于WPF comboBox selectedIndex = 0不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文