Bindings - toligmueller/WPF-MVVM-App-.NET5.0 GitHub Wiki

Bindings sind die Verbindung zwischen View und ViewModel.

View

Im View können diese an jeden beliebigen Parameter gebunden werden.

<Window x:Class="Beispiel.Views.MainWindowView"
	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	Title="MainViewWindow" 
        Height="300" 
        Width="300">
    <Grid>
	<Label Content={Binding Inhalt}/>
    </Grid>
</Window>

ViewModel

using Microsoft.Toolkit.Mvvm.ComponentModel;

namespace Beispiel.Viemodels
{
    public class BeispielViewModel : ObservableObject
    {
        public string Inhalt 
        {
            get => _inhalt;
            set => SetProperty(ref _inhalt, value);
        }
        private string _inhalt;
    }
}
using Microsoft.Toolkit.Mvvm.ComponentModel;

namespace Beispiel.Viemodels
{
    public class BeispielViewModel
    {
        public string Inhalt 
        {
            get => _inhalt;
            set 
            {
            	if (_inhalt != value){
                    OnPropertyChanged(nameof(Inhalt));
                    // Code der nur ausgeführt werden soll wenn "_inhalt" und "value" nicht gleich sind
                }
            }
        }
        private string _inhalt;
    }
}
using Microsoft.Toolkit.Mvvm.ComponentModel;

namespace Beispiel.Viemodels
{
    public class BeispielViewModel
    {
        public string Inhalt 
        {
            get => _inhalt;
            set
            {
                if (SetProperty(ref _inhalt, value)) 
                {
                    // Code der nur ausgeführt werden soll wenn "_inhalt" und "value" nicht gleich sind
                }
            }
        }
        private string _inhalt;
    }
}
⚠️ **GitHub.com Fallback** ⚠️