Ajouter un item qui n'existe pas dans le combobox MVVM
-
samedi 11 février 2012 22:35
j'ai un combobox attaché à une propriété ObservableCollection, tout va bien sauf je veux ajouter le cas suivant :
- Quand le choix n'existe pas dans la liste du combo l'utilisateur pourra l'insérer directement on le saisissant dans le combobox.
Comment implémenter ce cas avec l'approche MVVM ?
Merci.
- Modifié Hichem Sharaeen samedi 11 février 2012 22:41
Toutes les réponses
-
dimanche 12 février 2012 09:52Modérateur
Bonjour,
Tout simplement en créant une propriété "UserText" dans votre ViewModel et en la bindant sur le propriété "Text" de votre ComboBox dans votre View.
Cordialement
Gilles TOURREAU - MVP C#
Architecte logiciel/Consultant/Formateur Freelance
Blog : http://gilles.tourreau.fr
- MCPD : Enterprise Developper / Windows Developper 3.5 / ASP .NET 3.5/4.0
- MCITP : SQL Server 2008 Developper
- MCTS : ADO .NET 3.5 / SQL Server 2008 Developper / Windows Forms 3.5 / ASP .NET 3.5/4.0- Marqué comme réponse JonathanANTOINEMVP, Moderator dimanche 12 février 2012 13:29
-
dimanche 12 février 2012 13:30Modérateur
Bonjour,
Merci de partager avec nous les résultats,afin que d'autres personnes avec le même problème puissent profiter de cette solution.
Bonne fin de journée,
Jonathan ANTOINE - Découvrez mon livre sur MVVM: http://goo.gl/N6Tmn - http://www.jonathanantoine.com
-
dimanche 12 février 2012 18:44
Bonjour et merci pour la réponse, pour les résultat :
Code C# dans le viewModel : (SQLSnippetViewModel)
public class SQLSnippetViewModel : INotifyPropertyChanged {
public SQLSnippetViewModel()
{AddCategoryCommand = new AddCategoryCommand(this);
}
private string toInsertCategory; public string ToInsertCategory { get { return toInsertCategory; } set { toInsertCategory = value; OnPropertyChanged("ToInsertCategory"); } }
Code C# dans la class AddCategoryCommand.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Input; using System.Windows; namespace SQLTranactsStore.ViewModel { public class AddCategoryCommand : ICommand { SQLSnippetViewModel SQLSnippetViewModel; public AddCategoryCommand(SQLSnippetViewModel SQLSnippetViewModel) { this.SQLSnippetViewModel = SQLSnippetViewModel; } public bool CanExecute(object parameter) { if (String.IsNullOrEmpty(SQLSnippetViewModel.ToInsertCategory.Trim())) return false; foreach (var item in SQLSnippetViewModel.ListCategories) { if (SQLSnippetViewModel.ToInsertCategory == item.CategoryName) return false; } return true; } public event EventHandler CanExecuteChanged { add { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested -= value; } } public void Execute(object parameter) { if (MessageBox.Show("Do you confirm adding this new category ?", "Add", MessageBoxButton.YesNo) != MessageBoxResult.Yes) { SQLSnippetViewModel.ToInsertCategory = ""; return; } SQLSnippetViewModel.ListCategories.Add(new Category { CategoryName = SQLSnippetViewModel.ToInsertCategory, IDCategory = Guid.NewGuid() }); } } }et enfin XAML :
<combobox displaymemberpath="CategoryName" grid.column="4" grid.row="1" height="23" horizontalalignment="Right" iseditable="True" itemssource="{Binding ListCategories}" selectedindex="0" selectedvalue="{Binding SelectedSnippet.IDCategory}" selectedvaluepath="IDCategory" text="{Binding ToInsertCategory, Mode=TwoWay}" width="220"> <combobox.inputbindings> <keybinding command="{Binding AddCategoryCommand}" commandparameter="{Binding Path=Text, RelativeSource={RelativeSource AncestorType={x:Type ComboBox}}}" key="Enter"></keybinding> </combobox.inputbindings> </combobox>
- Modifié Hichem Sharaeen dimanche 12 février 2012 18:47

