POCO ViewModels
De Banane Atomic
Aller à la navigationAller à la recherche
Liaison du DataContext
MainWindow.xaml |
<Window xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm" xmlns:local="clr-namespace:MonNamespace" DataContext="{dxmvvm:ViewModelSource Type=local:MainWindowVM}"> |
MainWindowVM.cs |
using DevExpress.Xpf.Mvvm.POCO; // DevExpress.Mvvm.v15.1 + DevExpress.Xpf.Core.v15.1 namespace MonNamespace { public class MainWindowVM { // We recommend that you not use public constructors // to prevent creating the View Model without the ViewModelSource protected MainWindowVM() { } //This is a helper method that uses the ViewModelSource class for creating a LoginViewModel instance public static MainWindowVM Create() { return ViewModelSource.Create(() => new MainWindowVM()); } } } |
Propriétés bindables
Des propriétés bindables sont générées pour toutes les propriétés :
- auto-implémentés { get; set; } avec get public et set public ou protected
- virtuelles
MainWindowVM.cs |
public virtual string BindableProperty { get; set; } // marquer une propriété comme non bindable [BindableProperty(isBindable: false)] public virtual string NonBindableProperty { get; set; } // 2 méthodes peuvent être définies pour intercepter le changement de valeur de la propriété protected void OnBindablePropertyChanged(string oldValue) { } protected void OnBindablePropertyChanging(string newValue) { } // forcer le nom de la méthode [BindableProperty(OnPropertyChangedMethodName = "Update")] public virtual string BindableProperty { get; set; } protected void Update() { } |
Command
Des commandes sont générés pour toutes les méthodes:
- sans paramètre
- avec un paramètre
MainWindow.xaml |
<Button Content="bBb" Command="{Binding Path=DoItCommand}" /> |
MainWindowVM.cs |
// DoItCommand will be created for the DoIt and CanDoIt methods: // DoItCommand = new DelegateCommand<string>(DoIt, CanDoIt); public void DoIt(string paramater) { ... } public bool CanDoIt(string paramater) { returm true; } [Command(isCommand: false)] public void NotACommand() { } |
Erreurs
The tag ViewModelSource does not exist in XML namespace http://schemas.devexpress.com/winfx/2008/xaml/mvvm
Ajouter une référence à DevExpress.Xpf.Core.v15.1