Hello,
Firstly, you need to set the SelectedLanguage type to be consistent with the Model.
For example, you have a Model like following code.
public class MyModel
{
public string LanguageName { get; set; }
}
Then you need to set type of the SelectedLanguage to the MyModel
like following code.
internal class MyViewModel: INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged([CallerMemberName] string name = "") =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
private MyModel selectedLanguage;
public MyModel SelectedLanguage
{
get { return selectedLanguage; }
set { selectedLanguage = value;OnPropertyChanged(); }
}
public ObservableCollection<MyModel> SupportedLanguages { get; set; }
public MyViewModel()
{
SupportedLanguages=new ObservableCollection<MyModel>();
//add static value for testing.
for (int i = 0; i < 10; i++)
{
SupportedLanguages.Add( new MyModel() { LanguageName="test"+i });
}
}
}
For example, I want to set the SelectedLanguage that MyModel's LanguageName is "test1", then return back MyModel to the SelectedLanguage.
vm.SelectedLanguage = vm.SupportedLanguages.Where<MyModel>(t=>t.LanguageName.Equals("test1")).FirstOrDefault();
Best Regards,
Leon Lu
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.