C# WPF 组合框选择第一项 [英] C# WPF Combobox select first item

查看:20
本文介绍了C# WPF 组合框选择第一项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

再见,

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

I want my combobox to select the first item in it. I am using C# and WPF. I read the data from a DataSet. To fill the combobox:

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

组合框 XAML 代码:

Combo box XAML code:

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

如果我尝试:

cbGastid.SelectedIndex = 0; 

它不起作用.

推荐答案

使用以下内容更新您的 XAML:

Update your XAML with this:

<ComboBox 
        Name="cbGastid" 
        ItemsSource="{Binding}" 
        DisplayMemberPath="Description" 
        SelectedItem="{Binding Path=id}"
        IsSynchronizedWithCurrentItem="True"
        SelectedIndex="0" />  // Add me!

这篇关于C# WPF 组合框选择第一项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆