Verwenden von WPF DataGridComboBoxColumn mit MVVM - Bindung an Eigenschaft in ViewModel

Ich benutze das exzellente MVVM Light Toolkit. Mein ViewModel macht Folgendes verfügbar:

public const string CourtCodesTypeCourtPropertyName = "CourtCodesTypeCourt";
private List<CourtType> _courtCodesTypes = new List<CourtType>();
public List<CourtType> CourtCodesTypeCourt
{
    get
    {
        return _courtCodesTypes;
    }

    set
    {
        if (_courtCodesTypes == value)
        {
            return;
        }

        var oldValue = _courtCodesTypes;
        _courtCodesTypes = value;

        // Update bindings and broadcast change using GalaSoft.MvvmLight.Messenging
        RaisePropertyChanged(CourtCodesTypeCourtPropertyName, oldValue, value, true);
    }
}

public const string CourtCodesPropertyName = "CourtCodes";
private List<Court> _courtCodes = null;
public List<Court> CourtCodes
{
    get
    {
        return _courtCodes;
    }

    set
    {
        if (_courtCodes == value)
        {
            return;
        }

        var oldValue = _courtCodes;
        _courtCodes = value;

        // Update bindings and broadcast change using GalaSoft.Utility.Messenging
        RaisePropertyChanged(CourtCodesPropertyName, oldValue, value, true);
    }
}

The View hat ein DataGrid:

<DataGrid
      ItemsSource="{Binding CourtCodes, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
      AutoGenerateColumns="False"
      AlternatingRowBackground="{DynamicResource OffsetBrown}"
      AlternationCount="1" Margin="45,0">
   <DataGrid.Columns>
    <DataGridTextColumn Binding="{Binding Abbreviation, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
         Header="Abbreviation"
         Width="25*" />
    <DataGridTextColumn Binding="{Binding FullName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
         Header="Court"
         Width="75*" />
    <DataGridComboBoxColumn Header="CourtType" 
         ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.CourtCodesTypeCourt} TextBinding="{Binding CourtTypeDescription}""/>
   </DataGrid.Columns>
  </DataGrid>

Das DataGrid verfügt, wie Sie sehen, über eine ItemsSource von CourtCodes. Ich möchte, dass die CourtType-Spalte eine Dropdown-Liste aller in CourtCodesTypeCourt enthaltenen CourtTypes enthält. Für das Leben von mir kann ich nicht scheinen, die DataGridComboBoxColumn mit irgendetwas aufzufüllen. Der derzeitige fehlgeschlagene Versuch besteht darin, RelativeSource zu verwenden. Was mache ich falsch?

Die beiden Fehler, die ich sehe, funktionieren nicht nur, sondern sind auch:

System.Windows.Data Fehler: 4: Die Quelle für die Bindung mit dem Verweis 'RelativeSource FindAncestor, AncestorType =' System.Windows.Window ', AncestorLevel =' 1 '' kann nicht gefunden werden. BindingExpression: Path = DataContext.CourtCodesTypeCourt; DataItem = null; Zielelement ist 'DataGridComboBoxColumn' (HashCode = 38771709); Zieleigenschaft ist 'ItemsSource' (Typ 'IEnumerable')

un

System.Windows.Data Fehler: 40: BindingExpression-Pfadfehler: 'CourtCodesTypeCourt'-Eigenschaft für' Objekt '' 'Gericht' (HashCode = 38141773) 'nicht gefunden. BindingExpression: Path = CourtCodesTypeCourt.CourtTypeDescription; DataItem = 'Court' (HashCode = 38141773); Zielelement ist 'ComboBox' (Name = ''); Zieleigenschaft ist 'Text' (Typ 'String')

Antworten auf die Frage(6)

Ihre Antwort auf die Frage