[Prism] Detailed data binding of MVVM mode

1. Prism framework package installation

        First, Nuget installs the prism framework package, and then modifies the App class. Modify App to inherit PrismApplication and implement two virtual methods in PrismApplication. At the same time, modify the xaml file, add a namespace, xmlns:prism=" ", and modify Application to prism:PrismApplication.

Two, DataContext binding

        Prism provides automatic data binding function, only need to compound the corresponding naming convention. Among them, the View page needs to be placed in the Views folder and named NameView, and the ViewModel needs to be placed in the ViewModels folder, named NameViewModel, and needs to inherit the Bindablebase base class. Then in the XAML file add:

prism:ViewModelLocator.AutoWireViewModel="True"

         At this point DataContext is automatically bound to new NameViewModel

        Prism's MVVM pattern provides the following binding functions:

                Property binding, the dependency properties of the front-end space are bound to the back-end.

                Command binding, the binding of buttons or other spaces capable of executing commands to back-end commands.

                Command parameter binding, passing parameters like backend commands

                composite binding

3. Attribute binding

        Take TextBlock as an example, where the Text property is bound to Text="{Binding Name}". Text is bound to the Name property in the background

private string name;
public string Name
{
    get
    {
        return name;
    }
    set
    {
        SetProperty(ref name,value)
    }
}

        The SetProperty method here is in the Bindablebase class, which implements the INotifyPropertyChanged base class. There are also corresponding generic methods. The function of event notification has been realized. When the attribute is modified, the corresponding foreground attribute will also be modified

4. Command Binding

        The form of command binding Command="{Binding Click_Command}". The corresponding background code is:

public DelegateCommand Clicl_Command
{
    get => return new DelegateCommand( Action action );
}

        The creation form of DelegateCommand has direct creation, and a private execution method is added in the constructor, which is the method body of executing the Command. Or when creating, write a Lambda expression.

        When parameters need to be passed, the front-end code should add CommandParameter="{Binding ElementName=xName}" to bind a front-end control name. Corresponding control parameters can be passed to the backend.

Guess you like

Origin blog.csdn.net/weixin_43163656/article/details/127884731