C# WPF 组合框选择第一项

标签 c# wpf xaml combobox dataset

你好,

我希望我的组合框选择其中的第一项。我正在使用 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/

相关文章:

c# - 找不到引用 'RelativeSource FindAncestor' 的绑定(bind)源

c# - 在XAML中创建全屏GridView

xaml - Xamarin forms 选定选项卡的 Shell 自定义图标

c# - 按钮看起来已禁用,直到我单击某些内容

c# - 将按下的任何键绑定(bind)到 VM WPF 中的命令

c# - 微软 WPF 转换器

c# - 如何向 R# 指示函数检查变量是否为 null

c# - 温莎城堡 : is there a way of validating registration without a resolve call?

c# - 来自 C# 类的 JavaScript 警报

c# - EF Core 2.1 为一对多关系选择字段