Combobox中的数据绑定

编程入门 行业动态 更新时间:2024-10-09 21:24:00
本文介绍了Combobox中的数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我将数据库表的主键绑定到组合框的selectedIndex。问题发生在主键从1开始,而selectedIndex从0开始接受。我的意思是,当我想在数据库中看到ID = 1的项目时,由于它在索引为0的combobox中被列为第一个元素,它显示第二个元素在列表中,在组合框中的ID = 1。任何人都可以帮助我解决这个问题?

I binded a database table's primary key to the selectedIndex of a combobox. the problem occurs where the primary key starts from 1 but selectedIndex accepts from 0. I mean, when I want to see the item with ID=1 in database, since it's listed as first element in combobox with index 0, it displays the second element in the list, which is considered with ID=1 in the combobox. Can anyone help me on solving this problem?

提前感谢。 这里是我的组合框:

Thanks in advance. here's my combobox:

<ComboBox SelectedIndex="{Binding SC.User1.UserID, UpdateSourceTrigger=PropertyChanged }" IsSynchronizedWithCurrentItem="True" x:Name="proxyResponsibleUserCmb" ItemsSource="{Binding Users, Mode=OneTime}" SelectedItem="{Binding SC.User1.FullName, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}" Validation.ErrorTemplate="{x:Null}" Height="23" VerticalAlignment="Top" HorizontalAlignment="Left" Width="118" Margin="184,3,0,0" Grid.Row="0" Grid.Column="1"/>

推荐答案

使用ComboBox的 SelectedValuePath 和 DisplayMemberPath ,并使用SelectedValue而不是SelectedItem设置默认项目?

What about using the ComboBox's SelectedValuePath and DisplayMemberPath, and setting your default item with SelectedValue instead of SelectedItem?

<ComboBox x:Name="proxyResponsibleUserCmb" SelectedValuePath="{Binding UserID}" DisplayMemberPath="{Binding FullName}" SelectedValue="{Binding SC.User1.UserId, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}" ItemsSource="{Binding Users, Mode=OneTime}" />

更多推荐

Combobox中的数据绑定

本文发布于:2023-11-13 04:36:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1583447.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   数据   Combobox

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!