加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C#WPF组合框选择第一项

发布时间:2020-12-15 03:56:06 所属栏目:百科 来源:网络整理
导读:美好的一天, 我想要我的组合框选择其中的第一个项目.我正在使用C#和WPF.我从DataSet读取数据.要填充组合框: DataTable sitesTable = clGast.SelectAll().Tables[0];cbGastid.ItemsSource = sitesTable.DefaultView; 组合框XAML代码: ComboBox Name="cbGast
美好的一天,

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

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!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读