2017-03-23 22 views
1

내 DataGridComboBoxColumn에 데이터가 표시되지 않습니다. 그것은 비어 있습니다. ComboBox를 채우는 데 문제가 없지만 DataGridComboBoxColumn이 작동하지 않습니다.
.NetFramework 4.6.1DataGridComboBoxColumn이 비어 있습니다.

모델 :

public class Address 
{ 
    public string Firstname { get; set; } 
    public string Lastname { get; set; } 
    public string Country { get; set; } 
} 

뷰 모델 :

public class AddressViewModel 
{ 
    public AddressViewModel() 
    { 
     LoadAdresses(); 
     LoadCountries(); 
    } 

    public List<Address> AddressList { get; set; } 
    public List<string> CountryList { get; set; } 


    private void LoadAdresses() 
    { 
     AddressList = new List<Model.Address>(); 
     AddressList.Add(new Model.Address(){ Firstname = "Peter", Lastname = "R.", Country = "A" }); 
     AddressList.Add(new Model.Address(){ Firstname = "Tom", Lastname = "A.", Country = "A" }); 
     AddressList.Add(new Model.Address(){ Firstname = "Sam", Lastname = "F.", Country = "A" }); 
    } 

    private void LoadCountries() 
    { 
     CountryList = new List<string>(); 
     CountryList.Add("A"); 
     CountryList.Add("D"); 
     CountryList.Add("CH"); 
     CountryList.Add("GB"); 
     CountryList.Add("F"); 
    } 
} 

보기 :

<Window.DataContext> 
    <vm:AddressViewModel/> 
</Window.DataContext> 

<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto"/> 
     <RowDefinition Height="Auto"/> 
     <RowDefinition Height="*"/> 
    </Grid.RowDefinitions> 

    <DataGrid x:Name="AddressDataGrid" Grid.Row="0" ItemsSource="{Binding AddressList}" AutoGenerateColumns="False" > 
     <DataGrid.Columns> 
      <DataGridTextColumn Header="Firstname" Binding="{Binding Firstname}" />    
      <DataGridTextColumn Header="Lastname" Binding="{Binding Lastname}" /> 
      <DataGridComboBoxColumn Header="Country" 
         SelectedItemBinding="{Binding Country}" 
         ItemsSource="{Binding CountryList}"/> 
     </DataGrid.Columns> 
    </DataGrid> 

    <!--This ComboBox works--> 
    <ComboBox Grid.Row="1" ItemsSource="{Binding CountryList}"/> 
</Grid> 

이 동작에 대한 이유는 무엇입니까

?

답변

1

귀하의 DataGridComboBoxColumn에있는 속성 CountryList에 바인딩하고 있습니다. 하지만이 건물은 수업에 포함되어 있지 않습니다. Address; 수업 시간은 AddressViewModel입니다. 그게 왜 당신이 아무것도 보이지 않는지 ComboBox; 바인딩이 유효하지 않습니다.

당신의 DataGrid에서 한 행에 대한 List<Address> AddressList 스탠드에서

각 항목 당신은 '정상'이 {Binding Property} 바인딩 각 라인에 Address의 각 속성에 액세스 할 수 있습니다. 그러나 List<Address> AddressList을 보유하고있는 클래스에있는 속성에 액세스하려고합니다. 그래서 다른 방법으로 바인딩해야합니다. 두 가지 방법이 가능합니다 :

  • RelativeSource이가 작업을해야 ElementName

를 통해 바인딩

  • 결합하면

    <DataGridComboBoxColumn Header="Country" 
             SelectedItemBinding="{Binding Country}"> 
        <DataGridComboBoxColumn.ElementStyle> 
         <Style TargetType="{x:Type ComboBox}"> 
          <Setter Property="ItemsSource" 
            Value="{Binding Path=DataContext.CountryList, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" /> 
         </Style> 
        </DataGridComboBoxColumn.ElementStyle> 
        <DataGridComboBoxColumn.EditingElementStyle> 
         <Style TargetType="{x:Type ComboBox}"> 
          <Setter Property="ItemsSource" 
            Value="{Binding Path=DataContext.CountryList, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" /> 
         </Style> 
        </DataGridComboBoxColumn.EditingElementStyle> 
    </DataGridComboBoxColumn> 
    

    단지 힌트 : 당신은 당신의 뷰 사용 컬렉션을 결합하는 경우 보기를 최신 상태로 유지하려면 List 대신 ObservableCollection을 입력하십시오.

  • +0

    내 DataGrid는 List

    AddressList의 인스턴스에 바인딩됩니다. Address 클래스에는 Country 속성이 있습니다. DataGridComboBoxColumn은이 속성에 바인딩되며 ItemsSource에 List CountryList의 인스턴스를 사용하여 콤보 상자 컨트롤의 내용을 생성합니다.
    다른 콤보 상자가 작동합니다! – PeRa

    +0

    나는 당신의 코드를 보았습니다. :) 그리고 왜 DataGridComboBox가 작동하지 않는지 설명하려고했습니다. 올바른 DataContext에 있지 않기 때문입니다. 각 행에는 DataContext와 같은 Address 개체가 있으며 Address 개체에는 CountryList가 없습니다. 그것이 작동하지 않는 이유입니다. DataGrid 외부의 ComboBox에는 다른 DataContext가 있습니다. 내 xaml 코드를 사용해 보셨습니까? –

    +0

    감사합니다. 귀하의 xaml 작품과 나는 당신의 설명을 이해합니다. 꽤 복잡합니다. 즉, ItemsSource 속성을 사용하려면 StaticResource를 사용해야합니다. – PeRa