samedi 10 juillet 2021

does MVVM pattern in WPF allow to manage click event in axml?

How do events management work in WPF using MVVM pattern? Should i really avoid using a direct event management? EXAMPLE: is building a view like this:

<Grid>
    <Button Click="GO_Button_Click" Content="Go" Margin="39,143,503,250"/>
    <TextBox x:Name="outputFolder" Margin="321,182,35,211" Text="{Binding outputFolderPath}"/>
</Grid>

having a viewmodel like this:

using System.ComponentModel;
using System.Windows;

namespace FirstWpfApp
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        #region variables
        public string outputFolderPath { get; set; }
        #endregion

        #region events
        private void GO_Button_Click(object sender, RoutedEventArgs e)
        {
            outputFolderPath = "Some data";
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(outputFolderPath)));
        }

        #endregion

        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}

Wouldn't i have a better implementation of MVVM pattern avoiding to call "GO_Button_Click" method from view?

Aucun commentaire:

Enregistrer un commentaire