C#WPF组合框选择第一项

前端之家收集整理的这篇文章主要介绍了C#WPF组合框选择第一项前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
美好的一天,

我想要我的组合框选择其中的第一个项目.我正在使用C#和WPF.我从DataSet读取数据.要填充组合框:

@H_502_4@DataTable sitesTable = clGast.SelectAll().Tables[0]; cbGastid.ItemsSource = sitesTable.DefaultView;

组合框XAML代码

@H_502_4@<ComboBox Name="cbGastid" ItemsSource="{Binding}" DisplayMemberPath="Description" SelectedItem="{Binding Path=id}" IsSynchronizedWithCurrentItem="True" />

如果我尝试:

@H_502_4@cbGastid.SelectedIndex = 0;

它不工作

解决方法

使用以下方式更新您的XAML: @H_502_4@<ComboBox Name="cbGastid" ItemsSource="{Binding}" DisplayMemberPath="Description" SelectedItem="{Binding Path=id}" IsSynchronizedWithCurrentItem="True" SelectedIndex="0" /> // Add me!

猜你在找的C#相关文章