1

如何在 ViewModel.AddNewMovie() 中获取所选语言的 ID?这是我的第一次尝试。这很糟糕,因为它是索引,而不是 ID。

看法:

...
<ComboBox ItemsSource="{Binding LanguagesCollection}" 
SelectedIndex="{Binding NewMovieLanguage}"
DisplayMemberPath="L_Name" SelectedValuePath="L_Id"/>
...

视图模型:

...

public ObservableCollection<Languages> LanguagesCollection { get; private set; }
public ObservableCollection<Movies> MoviesCollection{ get; private set; }

...

_Entities = new MyAppEntities();
LanguagesCollection = new ObservableCollection<Languages>(_Entities.Languages);
MoviesCollection = new ObservableCollection<Movies>(_Entities.Movies.Include("Languages"));

...

private void AddNewMovie()
{
        OnPropertyChanged("NewMovieTitle");
        OnPropertyChanged("NewMovieLanguage");
    try
    {
        Movies NewMovie = new Movies 
        {
            M_Title = NewMovieTitle,
            M_Language_Id = Convert.ToInt32(NewMovieLanguage)+1,
        };

        _Entities.AddToMovies(NewMovie);
        MoviesCollection.Add(NewMovie);
        ...
    }
    catch
    {
        ...
    }
}

实体数据模型:

电影(表)

M_Id Int32

M_Title字符串

M_Language_Id Int32 fk

语言(表格)

L_Id Int32

L_Name字符串

- 数据 -

语言

1 英语

2 德语

3 法语

4 西班牙语

4

2 回答 2

0

哦,解决方法是:

<ComboBox ItemsSource="{Binding LanguagesCollection}" 
SelectedValue="{Binding NewMovieLanguage}"
DisplayMemberPath="L_Name" SelectedValuePath="L_Id"/>
于 2012-12-30T14:24:40.603 回答
0

请更改为:

<ComboBox ItemsSource="{Binding LanguagesCollection}" 
SelectedItem="{Binding NewMovieLanguage}"
DisplayMemberPath="L_Name" SelectedValuePath="L_Id"/>
于 2012-12-30T14:28:08.767 回答