milosev.com
  • Home
    • List all categories
    • Sitemap
  • Downloads
    • WebSphere
    • Hitachi902
    • Hospital
    • Kryptonite
    • OCR
    • APK
  • About me
    • Gallery
      • Italy2022
    • Curriculum vitae
      • Resume
      • Lebenslauf
    • Social networks
      • Facebook
      • Twitter
      • LinkedIn
      • Xing
      • GitHub
      • Google Maps
      • Sports tracker
    • Adventures planning
  1. You are here:  
  2. Home
  3. C#
  4. WPF

NotifyPropertyChanged

Details
Written by: Stanko Milosev
Category: WPF
Published: 25 January 2014
Last Updated: 06 April 2022
Hits: 5427

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.

Two ways of setting datacontext

Details
Written by: Stanko Milosev
Category: WPF
Published: 23 January 2014
Last Updated: 24 January 2016
Hits: 24709

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.

Simple binding

Details
Written by: Stanko Milosev
Category: WPF
Published: 22 January 2014
Last Updated: 30 November -0001
Hits: 4180

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>

Programatically binding two text boxes

Details
Written by: Stanko Milosev
Category: WPF
Published: 21 January 2014
Last Updated: 30 November -0001
Hits: 3831

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);
        }
  1. Drag'N'drop
  2. Multiline text box
  3. Read XML from Azure blob storage
  4. Treeview with Autofac

Page 11 of 12

  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12