2017-03-23 1 views
1

Meine DataGridComboBoxColumn zeigt keine Daten an. Es ist leer. Ich habe kein Problem, eine ComboBox zu füllen, aber DataGridComboBoxColumn funktioniert nicht.
.NetFramework 4.6.1DataGridComboBoxColumn ist leer

Modell:

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

Ansichtsmodell:

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"); 
    } 
} 

Ausblick:

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

Was ist der Grund für dieses Verhalten?

Antwort

1

Sie sind an eine Eigenschaft CountryList in Ihrer DataGridComboBoxColumn gebunden. Aber diese Eigenschaft ist nicht in Ihrer Klasse Address; es ist in deiner Klasse AddressViewModel. Deshalb sehen Sie nichts in Ihrem ComboBox; Die Bindung ist nicht gültig.

Jeder Eintrag in Ihrem List<Address> AddressList steht für eine Zeile in Ihrem DataGrid und Sie jede Eigenschaft Address mit dem ‚normalen‘ Bindung {Binding Property} in jeder Zeile zugreifen können. Sie möchten jedoch auf eine Eigenschaft zugreifen, die in der Klasse List<Address> AddressList enthalten ist. Deshalb müssen Sie eine andere Art zu binden verwenden. Zwei Wege sind möglich:

  • RelativeSource Bindung
  • Bindung über ElementName

Dies sollte für Sie arbeiten

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

Und nur ein Hinweis: Wenn Sie binden Sammlungen zu Ihrer Ansicht Einsatz ObservableCollection statt List, um Ihre Ansicht auf dem neuesten Stand zu halten.

+0

Mein DataGrid ist an eine Instanz der Liste

AddressList gebunden. Die Klasse Adresse hat eine Eigenschaft Land. Die DataGridComboBoxColumn ist an diese Eigenschaft gebunden und verwendet eine Instanz von List CountryList für ItemsSource, um den Inhalt des Kombinationsfeldsteuerelements zu generieren.
Das andere Kombinationsfeld funktioniert! – PeRa

+0

Ich weiß, ich habe Ihren Code gesehen :) Und ich habe versucht zu erklären, warum Ihre DataGridComboBox nicht funktioniert. Es liegt daran, dass Sie nicht im richtigen DataContext sind. Jede Zeile hat ein Address-Objekt als DataContext und es gibt keine CountryList in Ihrem Address-Objekt. Deshalb funktioniert es nicht. Die ComboBox außerhalb des DataGrid hat einen anderen DataContext. Hast du meinen XAML-Code ausprobiert? –

+0

Danke. Dein Xaml funktioniert und ich verstehe deine Erklärung. Ziemlich kompliziert. Das heißt, wenn ich die ItemsSource-Eigenschaft verwenden möchte, muss ich mit einer StaticResource arbeiten? – PeRa