我已經看過variousquestions的答案,但還沒有設法將答案中的內容映射到我試圖解決的問題上。我已經將它縮減爲下面的代碼(代表我嘗試實現的結果),並且基本上希望能夠在行未被編輯時呈現Person.TitleId
作爲其對應的Title.TitleText
,並且具有下降正確綁定,以便它在下拉列表中顯示TitleText
,並在關聯的TitleId
記錄更改後將其寫回Person
記錄。用MVVM綁定WPF DataGridComboBoxColumn
總之,我爲了達到這個目的在<DataGridComboBoxColumn>
中放什麼?
App.xaml.cs
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
var viewModel = new ViewModels.MainWindowViewModel();
var mainWindow = new MainWindow();
mainWindow.DataContext = viewModel;
mainWindow.ShowDialog();
}
MainWindow.xaml
<Grid>
<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Path=Contacts}">
<DataGrid.Columns>
<DataGridComboBoxColumn Header="Title" SelectedItemBinding="{Binding Person}">
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.Titles}"/>
<Setter Property="IsReadOnly" Value="True"/>
</Style>
</DataGridComboBoxColumn.ElementStyle>
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.Titles}"/>
<Setter Property="DisplayMemberPath" Value="TitleText" />
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
Person.cs
public class Person
{
public int TitleId { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
}
Title.cs
public struct Title
{
public Title(int titleId, string titleText)
: this()
{
TitleId = titleId;
TitleText = titleText;
}
public string TitleText { get; private set; }
public int TitleId { get; private set; }
public static List<Title> GetAvailableTitles()
{
var titles = new List<Title>();
titles.Add(new Title(1, "Mr"));
titles.Add(new Title(2, "Miss"));
titles.Add(new Title(3, "Mrs"));
return titles;
}
}
MainWindowViewModel.cs
public class MainWindowViewModel : ViewModelBase
{
private ObservableCollection<Person> contacts;
private List<Title> titles;
public MainWindowViewModel()
{
titles = Title.GetAvailableTitles();
Contacts = new ObservableCollection<Person>();
Contacts.Add(new Person() { FirstName = "Jane", LastName = "Smith", TitleId = 2 });
}
public List<Title> Titles
{
get { return titles; }
}
public ObservableCollection<Person> Contacts
{
get { return contacts; }
set
{
if (contacts != value)
{
contacts = value;
this.OnPropertyChanged("Contacts");
}
}
}
}
ViewModelBase.cs
public class ViewModelBase : INotifyPropertyChanged
{
protected void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
}
如果你能概述那個「計劃」的哪些部分不起作用/哪些實際上做了,它將會有所幫助。例如你能找到ComboBox的ItemsSource列表嗎? – 2011-04-01 19:32:49
另外:從看起來你如何使用'Title'這個類似乎是多餘的,並且最好由'Dictionary'替代。 –
2011-04-01 19:38:51
@ H.B。 - 如代碼所示,網格不顯示初始值的文本表示(即Person.TitleId的相應TitleText),下拉列表中正確填充{Mr,Mrs,Miss}並選擇一個項目下拉式結果在TestMVVM.Models中。標題顯示在網格中(TestMVVM.Models是解決方案中的名稱空間,爲簡潔起見,我將其刪除)。 – Rob 2011-04-01 19:39:26