How do I get the Id of the selected language in the ViewModel.AddNewMovie()? This is my first try. It's bad, because it's the Index, and isn't the Id.
如何在ViewModel.AddNewMovie()中获取所选语言的ID?这是我的第一次尝试。这很糟糕,因为它是索引,而不是Id。
View:
...
<ComboBox ItemsSource="{Binding LanguagesCollection}"
SelectedIndex="{Binding NewMovieLanguage}"
DisplayMemberPath="L_Name" SelectedValuePath="L_Id"/>
...
ViewModel:
...
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
{
...
}
}
Entity DataModel:
Movies (table)
M_Id Int32
M_Title String
M_Language_Id Int32 fk
M_Language_Id Int32 fk
Languages (table)
L_Id Int32
L_Name String
-- Data --
- 数据 -
Languages
1 English
2 German
3 French
4 Spanish
2 个解决方案
#1
0
Oh, the solution is:
哦,解决方案是:
<ComboBox ItemsSource="{Binding LanguagesCollection}"
SelectedValue="{Binding NewMovieLanguage}"
DisplayMemberPath="L_Name" SelectedValuePath="L_Id"/>
#2
0
please change same as:
请改为:
<ComboBox ItemsSource="{Binding LanguagesCollection}"
SelectedItem="{Binding NewMovieLanguage}"
DisplayMemberPath="L_Name" SelectedValuePath="L_Id"/>
#1
0
Oh, the solution is:
哦,解决方案是:
<ComboBox ItemsSource="{Binding LanguagesCollection}"
SelectedValue="{Binding NewMovieLanguage}"
DisplayMemberPath="L_Name" SelectedValuePath="L_Id"/>
#2
0
please change same as:
请改为:
<ComboBox ItemsSource="{Binding LanguagesCollection}"
SelectedItem="{Binding NewMovieLanguage}"
DisplayMemberPath="L_Name" SelectedValuePath="L_Id"/>