2015-01-29 1 views
0
<DataGridComboBoxColumn x:Name="categoryColumn" Header="Category"  
            SelectedValueBinding="{Binding CategoryID}" 
            SelectedValuePath="CategoryID" 
            DisplayMemberPath="CategoryName" 
            Width="200"> 

DataGridComboBoxColumn 내가 DataGridComboboxCOlumn에 데이터/항목 소스를 바인딩 할 수 없습니다 오전 WPF에 새가되는

categoryColumn.ItemsSource = FetchData.CategoriesList; 
List<FileModel> _files = new List<FileModel>(); 
     _files.Clear(); 
     _files.Add(new FileModel 
     { 
      Filename = "Test.pdf", 
      Title = "Test", 
      Category = new CategoryModel 
      { 
       CategoryID = 63, 
       CategoryName = "Personal" 
      } 
     }); 
     DataGrid.ItemsSource = _files; 

. 여기에서는 콤보 상자가 전혀 보이지 않습니다. 도와주세요. 이들이 동일한 비주얼 트리의 일부가 아니기 때문에

답변

1

문제는 데이터 그리드의 데이터 컨텍스트가 있습니다 .. DataGridComboBoxBolumn에 전달되지 않는 점이다.

그래서 ... 당신은 데이터 그리드 내에서 CategoryModel의 값에 바인딩 할 때 ... 그것을 찾을 수 없습니다. 당신의 CategoriesList을, 당신은이 방법을 사용할 수 있습니다

<!—now itemssource will find the correct DataContext--> 
<dg:DataGridComboBoxColumn Header="Current Product" 
    SelectedValueBinding="{Binding Path=CurrentProduct}" 
     SelectedValuePath="ProductID" 
    DisplayMemberPath="ProductName">    
    <dg:DataGridComboBoxColumn.ElementStyle> 
    <Style TargetType="ComboBox"> 
     <Setter Property="ItemsSource" Value="{Binding Path=ProductsInCategory}" /> 
    </Style> 
    </dg:DataGridComboBoxColumn.ElementStyle> 
    <dg:DataGridComboBoxColumn.EditingElementStyle> 
    <Style TargetType="ComboBox"> 
     <Setter Property="ItemsSource" Value="{Binding Path=ProductsInCategory}" /> 
    </Style> 
    </dg:DataGridComboBoxColumn.EditingElementStyle> 
</dg:DataGridComboBoxColumn> 

:

Here

는 데이터 그리드와 같은 시각적 트리의 열 부분을함으로써 데이터 컨텍스트를 전달 ElementStyles를 사용하여이 문제에 대한 하나의 방법입니다 당신이 결합 할 수있는 재산 : 당신의 설정 코드에서 다음

public ObservableCollection<CategoryModel> CategoriesList { get; set; } 

:

CategoriesList = FetchData.CategoriesList; 
,536,

(위의 예에서는 ComboBox의 ItemsSource를 "ProductsInCategory"대신 "CategoriesList"에 바인딩합니다.)

+0

감사합니다. Gordon .. –

관련 문제