我之前读过很多文章来问这个问题,但最后我决定在这里写,因为我找不到适合我的解决方案的文章。
我DataGrid
在视图上有以下内容:
<DataGrid x:Name="UsersCollection" CanUserAddRows="False" CanUserDeleteRows="False" AutoGenerateColumns="False" HorizontalAlignment="Center" VerticalAlignment="Top" Height="525" Width="510" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Canvas.Left="147" Canvas.Top="26" Margin="0">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Username, Mode=TwoWay, UpdateSourceTrigger=LostFocus}"/>
<DataGridTextColumn Binding="{Binding Name, Mode=TwoWay, UpdateSourceTrigger=LostFocus}"/>
<DataGridComboBoxColumn x:Name="Languages" />
</DataGrid.Columns>
这是我的 ViewModel 的一部分:
private List<string> _languages;
[ImportingConstructor]
public UsersViewModel()
{
DisplayName = "Users";
_usersCollection = new TrulyObservableCollection<UserModel>();
foreach (UserModel user in GetAllUsers())
_usersCollection.Add(user);
_usersCollection.CollectionChanged += Users_CollectionChanged;
_usersCollection.ItemPropertyChanged += PropertyChangedHandler;
_languages = new List<string>();
_languages.Add("es");
_languages.Add("ca");
_languages.Add("en");
}
public List<string> Languages
{
get { return _languages; }
}
我的 UserModel 有这个属性:
public string Language
{
get { return _language; }
set
{
if (_language != value)
{
_language = value;
NotifyOfPropertyChange(() => Language);
}
}
}
如何绑定DataGrid
组合(当然,与 UserModel 的 Language 属性相关)?提前致谢!