gpt4 book ai didi

wpf - ComboBox和 Entity Framework ,如何获取ID? (WPF-MVVM)

转载 作者:行者123 更新时间:2023-12-03 10:48:54 24 4
gpt4 key购买 nike

如何在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
{
...
}
}

实体数据模型:

电影(表格)

M_Id Int32

M_标题字符串

M_Language_Id Int32 fk

语言(表)

L_Id Int32

L_Name字符串

-数据-

语言

1英文

2德语

3法语

4西类牙语

最佳答案

哦,解决方法是:

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

关于wpf - ComboBox和 Entity Framework ,如何获取ID? (WPF-MVVM),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14091333/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com