c# - 自动选择组合框的第一项

标签 c# wpf

我有一个绑定(bind) ViewModel 属性的 ComboBox。

<ComboBox  ItemsSource="{Binding UserCollection}"   SelectedValuePath="Id" DisplayMemberPath="UserName"   SelectedValue="{Binding SelectedUser}"       />

我想在它有项目时自动选择它的第一个项目。

我使用 IsSynchronizedWithCurrentItem="True"SelectedIndex="0" 但不选择项目。

最佳答案

绑定(bind) SelectedItem 而不是 SelectedValue :

<ComboBox ItemsSource="{Binding UserCollection}"  
          SelectedItem="{Binding SelectedUser}"     
          SelectedValuePath="Id"
          DisplayMemberPath="UserName"  
 />

然后将您的 SelectedUser 设置为列表的第一项(如果尚未设置):

SelectedUser = UserCollection[0];

关于c# - 自动选择组合框的第一项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30816113/

相关文章:

c# - Bot Framework V4 IActivityLogger

c# - Dynamodb : No defined key schema. 必须为所有表定义至少包含哈希键的键模式

c# - ListView + 多选 + MVVM =?

c# - 从 TreeView 获取 SelectedItem?

c# - 类似资源管理器的图像浏览器的最佳控件

c# - 将 WPF DataGrid ItemsSource 绑定(bind)到自定义 Table 对象的方法 IEnumerable<TableRow> GetRows()?

C# 委托(delegate)、dll、动态内容

c# - JsonConvert.DeserializeObject 上的 StackOverflowException

c# - .NET Core 中 Microsoft Fakes 的任何替代方案?

.net - 如何在 VS2008 中禁用 Cider(WPF Gui 编辑器)?