【问题标题】:Data Binding in Combobox组合框中的数据绑定
【发布时间】:2010-08-26 07:14:39
【问题描述】:

我将数据库表的主键绑定到组合框的 selectedIndex。问题出现在主键从 1 开始但 selectedIndex 从 0 接受的情况下。我的意思是,当我想在数据库中查看 ID=1 的项目时,因为它被列为索引为 0 的组合框中的第一个元素,它显示第二个元素在列表中,组合框中的 ID=1。谁能帮我解决这个问题?

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

<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"/>

【问题讨论】:

  • 你能提供一些代码吗?例如。如果您的 selectedIndex 是一个属性,为什么不在那里进行计算?

标签: wpf data-binding combobox


【解决方案1】:

如何使用组合框的SelectedValuePathDisplayMemberPath,并使用 SelectedValue 而不是 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}" />

【讨论】:

    【解决方案2】:

    将属性 IsSynchronizedWithCurrentItem(在您的 XAML 中)设置为 True 有帮助吗?

    编辑 也许这个链接会有所帮助:

    http://social.msdn.microsoft.com/Forums/en/wpf/thread/b4e84ea2-9597-4af1-8d3c-835b972e3d73

    【讨论】:

      【解决方案3】:

      通过 ValueConverter 快速解决方法:

      在您的代码隐藏中创建一个 ValueConverter:

      // of course use your own namespace...
      namespace MyNameSpace
      {
      public class IndexConverter : IValueConverter
      {
          public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
          {
              if(!(value is int)) // Add the breakpoint here!!
                  throw new Exception();
              int newindex = ((int)value - 1;
              return newindex;
          }
      
          public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
          {
              throw new NotImplementedException("This method should never be called");
          }
      }
      }
      

      然后,在您的 XAML 中显示它:

      //(declare a namespace in your window tag:)
      xmlns:myNamespace="clr-namespace:MyNameSpace"
      
      // add:
      <Window.Resources>
          <ResourceDictionary>
              <myNamespace:IndexConverter x:Key="indexConverter" />
          </ResourceDictionary>
      </Window.Resources>
      

      然后更改您的绑定:

      <ComboBox SelectedIndex="{Binding SC.User1.UserID, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource indexConverter}}" ... />
      

      这应该可以解决问题。至少可以通过在 IndexConverter 中插入断点来调试它。

      【讨论】:

      • 感谢您的解决方案,但我意识到当从数据库中删除用户时该技巧将失败,因此订单不再与组合框的索引同步。所以我仍然需要另一种解决方案才能选择正确的解决方案。
      • 现在我可以更新选定的项目 - 我可以通过调试看到它 - 但我无法在组合框中看到它被选中。组合框中没有显示任何内容。您知道它为什么会发生以及如何解决吗?
      • 如果您希望您的组合框在不重新加载页面的情况下更新数据更改,您应该考虑在两者之间放置一个 ViewModel 并绑定到其 ObservableCollection 或至少绑定到可以引发 OnPropertyChanged 事件的某个属性。我的解决方案对于静态数据库数据来说是快速的'n'dirty。
      猜你喜欢
      • 2016-05-02
      • 2012-01-24
      • 1970-01-01
      • 2017-11-16
      • 2013-04-23
      • 1970-01-01
      • 2011-09-06
      • 2011-08-15
      相关资源
      最近更新 更多