- Details
- Written by: Stanko Milosev
- Category: WPF
- Hits: 6099
Start new WPF application, add new model (class), like:
public class MyNotifyModel: INotifyPropertyChanged { private string myName; public string MyName { get { return myName; } set { myName = value; OnPropertyChanged("test"); } } protected void OnPropertyChanged(string name) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) { handler(this, new PropertyChangedEventArgs(name)); if (name == "test") { MessageBox.Show("tttets"); } } } public event PropertyChangedEventHandler PropertyChanged; }
Where XAML is like:
<Window x:Class="IMyNotify.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:system="clr-namespace:System;assembly=mscorlib" xmlns:iMyNotify="clr-namespace:IMyNotify" Title="MainWindow" Height="350" Width="525"> <Window.Resources> <iMyNotify:MyNotifyModel x:Key="IMyNotify" /> </Window.Resources> <Grid DataContext="{StaticResource IMyNotify}"> <TextBox HorizontalAlignment="Left" Height="23" Margin="159,67,0,0" TextWrapping="Wrap" Text="{Binding MyName, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="120"/> </Grid> </Window>
Important line to notice is:
UpdateSourceTrigger=PropertyChanged - that means that onPropertyChanged event will be raised when someone enters something in textbox field
Example you can download from here.
Now, if we start from scratch, your model should look like:
public class MyNotifyModel { public string MyName { get; set; } }
Now, lets add INotifyPropertyChanged, like:
public class MyNotifyModel: INotifyPropertyChanged { public string MyName { get; set; } }
Now we will have to implement members, so now model will look like:
public class MyNotifyModel:INotifyPropertyChanged { public string MyName { get; set; } public event PropertyChangedEventHandler PropertyChanged; }
After that you have to introduce new variable, in my case that was myName, set getters and setters as I already describe it on the beggining, and implement OnPropertyChanged event.
---
2014-06-13 Update Better version of notify property changed explanation you can see here.
- Details
- Written by: Stanko Milosev
- Category: WPF
- Hits: 25654
Both of these examples are with default value, you will find it in the constructor of model "Hello world!"
XAML datacontext
Add new WPF application project, add new model like:
namespace HelloWorldDataContext { public class HelloWorldDataContextModel { public string HelloWorld { get; set; } public HelloWorldDataContextModel() { HelloWorld = "Hello world!"; } } }
Then XAML:
<Window x:Class="HelloWorldDataContext.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:HelloWorldDataContext" Title="MainWindow" Height="350" Width="525"> <Window.Resources> <local:HelloWorldDataContextModel x:Key="HelloWorldDataContext" /> </Window.Resources> <Grid DataContext="{StaticResource HelloWorldDataContext}"> <TextBox HorizontalAlignment="Left" Height="23" Margin="222,127,0,0" TextWrapping="Wrap" Text="{Binding HelloWorld}" VerticalAlignment="Top" Width="120"/> </Grid> </Window>
Important lines in XAML on which you should take care are:
xmlns:local="clr-namespace:HelloWorldDataContext"
Where name space is declared in model like:
namespace HelloWorldDataContext
<local:HelloWorldDataContextModel x:Key="HelloWorldDataContext" />
<Grid DataContext="{StaticResource HelloWorldDataContext}">
Example project you can download from here.
Programmatically setting datacontext
XAML:
<Window x:Class="ProgramaticallyDataContext.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <TextBox HorizontalAlignment="Left" Height="23" Margin="147,78,0,0" TextWrapping="Wrap" Text="{Binding HelloWorld}" VerticalAlignment="Top" Width="120"/> </Grid> </Window>
Test Model is same like in above example, code behind (MainWindow.xaml.cs) is like:
public MainWindow() { InitializeComponent(); HelloWorldDataContextModel objHW = new HelloWorldDataContextModel(); objHW.HelloWorld = "Hello world"; this.DataContext = objHW; }
Example you can download from here.
- Details
- Written by: Stanko Milosev
- Category: WPF
- Hits: 4876
All you need is one XAML and two text boxes. So here goes XAML:
<Window x:Class="WpfApplication3.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <TextBox HorizontalAlignment="Left" Height="23" Margin="96,91,0,0" TextWrapping="Wrap" Text="{Binding Path=Text, ElementName=myTest}" VerticalAlignment="Top" Width="120"/> <TextBox HorizontalAlignment="Left" Height="23" Margin="120,200,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120" x:Name="myTest"/> </Grid> </Window>
- Details
- Written by: Stanko Milosev
- Category: WPF
- Hits: 4449
XAML:
<Window x:Class="WpfApplication2.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <TextBox x:Name="t1" HorizontalAlignment="Left" Height="23" Margin="101,43,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/> <TextBox x:Name="t2" HorizontalAlignment="Left" Height="23" Margin="101,91,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/> </Grid> </Window>
Code:
public MainWindow() { InitializeComponent(); Binding binding = new Binding(); binding.Source = t1; binding.Path = new PropertyPath(TextBox.TextProperty); BindingOperations.SetBinding(t2, TextBox.TextProperty, binding); }