2009-11-11 150 views
1

您好在WPF中的一个快速问题。WPF绑定到组合框

有谁知道我怎么可以绑定一个组合框,将坐在一个网格到我有一个数据集。

该数据集包含几列和数行,但我想要采取其中一列,让我们说ProcessID,并只显示在COMOBO框。

有没有人以前做过或知道它如何在WPF中完成。

谢谢 Iffy。

+0

读取后你的帖子再次,你的意思是数据集将绑定到一个DataGrid,组合将占据该DataGrid的列中的每个单元格?或者你的意思是组合只是坐在一个网格? (两者之间有很大的区别)。下面的答案是当组合是页面正常流程的一部分时,即它在网格中。 – slugster 2009-11-11 12:30:20

+0

该组合只是坐在一个网格,我只需要加载一些值:-) – Vytas 2009-11-16 11:46:15

回答

0

您应该使用LINQ到SQL,ADO.Net或实体框架,以提取数据库中的数据,你不能在内存绑定数据库表的组合框,但丝毫数据

1

要做到这一点编程,这里的一个通用的功能,我有它:

/// <summary> 
    /// Thread safe method for databinding the specified ComboBox with the specified data. 
    /// </summary> 
    /// <typeparam name="T"></typeparam> 
    /// <param name="ctrl">The Combo to be databound.</param> 
    /// <param name="datasource">The data to be bound to the Combo.</param> 
    /// <param name="displayPath">The name of the property in the data objects that should be used for the display value.</param> 
    /// <param name="valuePath">The name of the property in the data objects that should be used for the value.</param> 
    /// <remarks> 
    /// This function was written as generic so that it doesn't matter what types the IEnumerabe datasource is, it can handle them all. 
    /// </remarks> 
    private void UpdateComboDataSource<T>(ComboBox ctrl, IEnumerable<T> datasource, string displayPath, string valuePath) 
    { 
     if (ctrl == null) 
      throw new ArgumentNullException("Control to be bound must not be null"); 

     //check if we are on the control's UI thread: 
     if (ctrl.Dispatcher.CheckAccess()) 
     { 
      //we have full access to the control, no threading issues, so let's rip it up and databind it 
      datasource = datasource.OrderBy(x => x); 
      if (displayPath != null && ctrl.DisplayMemberPath == null) 
       ctrl.DisplayMemberPath = displayPath; 
      if (valuePath != null && ctrl.SelectedValuePath == null) 
       ctrl.SelectedValuePath = valuePath; 

      ctrl.ItemsSource = datasource; 

      //be nice to the user, if there is only one item then automatically select it 
      ctrl.SelectedIndex = datasource.Count() == 1 ? 0 : -1; 
     } 
     else 
     { 
      //we don't have full access to the control because we are running on a different thread, so 
      //we need to marshal a call to this function from the control's UI thread 
      UpdateComboDataSourceDelegate<T> del = new UpdateComboDataSourceDelegate<T>(this.UpdateComboDataSource); 
      ctrl.Dispatcher.BeginInvoke(del, ctrl, datasource, displayPath, valuePath); 
     } 
    } 

    private delegate void UpdateComboDataSourceDelegate<T>(ComboBox ctrl, IEnumerable<T> dataSource, string displayPath, string valuePath); 

评论告诉你你需要知道的一切。它也可以通过在控件的ItemSource属性和页面上的公共属性之间创建一个Binding来完成 - 这与它声明式的做法是一样的(尽管如此,必须有十亿个例子,所以我赢了'在这里展示)。

+0

感谢您的帮助。对此,我真的非常感激。 :) – Iffy 2009-11-11 14:24:56

2

假设这是一个大写d数据集可以暴露于XAML作为一个DataContext对象上的属性(表示“LookupTable中”表每行的“描述”栏):

<ComboBox ItemsSource="{Binding Path=MyDataSetExposedAsAProperty.Tables[LookupTable].Rows}" 
DisplayMemberPath=".[Description]"/>