Combobox中的数据绑定 [英] Data Binding in Combobox

查看:122
本文介绍了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中的数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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