2009-11-11 9 views
1

안녕하세요 WPF에서 정말 빠른 질문.콤보 상자에 바인딩 WPF 바인딩

누구나 내가 가지고있는 데이터 집합에 그리드에 앉아 콤보 상자를 바인딩 할 수있는 방법을 알고 있습니다.

데이터 세트에는 여러 개의 열과 많은 행이 있지만 하나의 열을 가져 와서 ProcessID라고 말하면서 comobo 상자에 표시하고 싶습니다.

누구든지 WPF에서이 작업을 수행하거나 수행하는 방법을 알고 있습니까?

감사합니다. Iffy.

+0

귀하의 게시물을 다시, 당신은 데이터 집합이 DataGrid에 바인딩된다는 것을 의미합니까 콤보는 해당 DataGrid의 열에있는 각 셀을 차지합니까? 또는 콤보가 Grid에 앉아 있다는 것을 의미합니까? (둘 사이에는 큰 차이가 있습니다). 아래의 내 대답은 콤보가 페이지의 정상적인 흐름의 일부인 경우 (즉, 그리드에있는 경우)입니다. – slugster

+0

콤보는 그리드에 앉아서 일부 값을로드해야합니다 :-) – Vytas

답변

0

당신은, 당신은 메모리에 데이터베이스 테이블과 콤보, 그러나 오순절 데이터를 바인딩 할 수 없습니다 데이터베이스에서 데이터를 추출하는 SQL, ADO.Net 또는 엔티티 프레임 워크에 LINQ를 사용해야은

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 속성과 페이지의 public 속성 사이에 Binding을 작성하여 프로그래밍 방식으로 프로그래밍 할 수도 있습니다. 이는 선언적으로 수행되는 것과 같은 방식입니다 (10 억 개의 예제가 있어야하므로 ' 여기에 그것을 보여주지 마라).

+0

도움을 주셔서 감사합니다. 정말 감사. :) – Iffy

2

이 당신이의 DataContext 개체의 속성으로 XAML에 노출 할 수있는 자본 D 데이터 집합 (이하 "LookupTable의"테이블의 각 행에 대해 "설명"열을 표시)입니다 가정 : 독서 후

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