Förstå grunderna i data binding i WPF och lär dig MVVM (Model-View-ViewModel)-designmönstret.
Data binding är en teknik som kopplar data mellan UI-element och logik i bakgrunden (vanligtvis en ViewModel eller en datakälla).
Starta ett nytt WPF-projekt:
Lägg till en egenskap i MainWindow.xaml.cs:
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace DataBindingDemo
{
public partial class MainWindow : Window, INotifyPropertyChanged
{
private string _textData;
public string TextData
{
get { return _textData; }
set
{
_textData = value;
OnPropertyChanged();
}
}
public MainWindow()
{
InitializeComponent();
DataContext = this;
TextData = "Hej, WPF!";
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
TextData.<Window x:Class="DataBindingDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Data Binding Demo" Height="350" Width="525">
<StackPanel Margin="20">
<TextBox Text="{Binding TextData, UpdateSourceTrigger=PropertyChanged}" Margin="0,0,0,10" />
<TextBlock Text="{Binding TextData}" FontSize="16" FontWeight="Bold" />
</StackPanel>
</Window>
MVVM (Model-View-ViewModel) är ett designmönster som separerar logik (ViewModel) från presentation (View) och data (Model).
Person.cs:namespace DataBindingDemo
{
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
}
MainViewModel.cs:using System.Collections.ObjectModel;
using System.ComponentModel;
namespace DataBindingDemo
{
public class MainViewModel : INotifyPropertyChanged
{
private string _searchText;
public string SearchText
{
get { return _searchText; }
set
{
_searchText = value;
OnPropertyChanged();
}
}
public ObservableCollection<Person> People { get; set; }
public MainViewModel()
{
People = new ObservableCollection<Person>
{
new Person { FirstName = "Anna", LastName = "Svensson" },
new Person { FirstName = "Björn", LastName = "Andersson" },
new Person { FirstName = "Cecilia", LastName = "Johansson" }
};
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
DataContext till ViewModel.public MainWindow()
{
InitializeComponent();
DataContext = new MainViewModel();
}
<Window x:Class="DataBindingDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MVVM Demo" Height="350" Width="525">
<StackPanel Margin="20">
<TextBox Text="{Binding SearchText, UpdateSourceTrigger=PropertyChanged}" PlaceholderText="Sök" Margin="0,0,0,10" />
<ListBox ItemsSource="{Binding People}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding FirstName}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</Window>
Nästa lektion kommer vi att fördjupa oss i stilar, templates och resurser för att skapa en mer visuellt tilltalande applikation.