你好,
我希望我的组合框选择其中的第一项。我正在使用 C# 和 WPF。我从数据集中读取数据。填充组合框:
DataTable sitesTable = clGast.SelectAll().Tables[0];
cbGastid.ItemsSource = sitesTable.DefaultView;
组合框 XAML 代码:
<ComboBox
Name="cbGastid"
ItemsSource="{Binding}"
DisplayMemberPath="Description"
SelectedItem="{Binding Path=id}"
IsSynchronizedWithCurrentItem="True" />
如果我尝试:
cbGastid.SelectedIndex = 0;
它不起作用。
最佳答案
用这个更新你的 XAML
:
<ComboBox
Name="cbGastid"
ItemsSource="{Binding}"
DisplayMemberPath="Description"
SelectedItem="{Binding Path=id}"
IsSynchronizedWithCurrentItem="True"
SelectedIndex="0" /> // Add me!
关于C# WPF 组合框选择第一项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20479976/