我使用了優秀的MVVM Light Toolkit。我的視圖模型公開:在MVVM中使用WPF DataGridComboBoxColumn - 在ViewModel中綁定到屬性
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);
}
}
視圖有一個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>
數據網格有一個ItemsSource時,你可以看到CourtCodes的。我希望CourtType列是CourtCodesTypeCourt中包含的所有列舉的CourtType的下拉列表。對於我的生活,我似乎無法用任何東西填充DataGridComboBoxColumn。當前失敗的嘗試正在尋找使用RelativeSource ...我做錯了什麼?
除了不能工作,這兩個錯誤,我看到的是:
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Window', AncestorLevel='1''. BindingExpression:Path=DataContext.CourtCodesTypeCourt; DataItem=null; target element is 'DataGridComboBoxColumn' (HashCode=38771709); target property is 'ItemsSource' (type 'IEnumerable')
和
System.Windows.Data Error: 40 : BindingExpression path error: 'CourtCodesTypeCourt' property not found on 'object' ''Court' (HashCode=38141773)'. BindingExpression:Path=CourtCodesTypeCourt.CourtTypeDescription; DataItem='Court' (HashCode=38141773); target element is 'ComboBox' (Name=''); target property is 'Text' (type 'String')
感謝您的幫助,肯特......這絕對讓我更加接近。我同意......不得不訴諸這種語法是荒謬的。我正在掙扎。 我現在有了填充DataGridComboBoxColumn的CourtType對象,但在這個語法中,我該如何控制顯示的文本?目前它列出了類型名稱,而不是CourtTypeDescription屬性。其次,我感謝您對最佳做法的反饋......我可能錯誤地認爲,MVVM Light Toolkit使mvvminpc片段創建了ObservableCollections中的特性......我是否也這麼做? – 2010-08-25 21:06:57
忽略ObservableCollections最佳實踐問題...我測試了一下並查看了值。掛鉤到CollectionChanged事件的能力似乎非常值得它本身。 我可以使用DataGridComboBoxColumn的語法輕推...再次感謝您的幫助! – 2010-08-26 04:07:28
@Mike:你應該可以在你的DataGridComboBoxColumn上設置DisplayMemberPath =「CourtTypeDescription」。 – 2010-08-26 08:52:10