XAML simplification of usage INotifyPropertyChanged

Hello,

I want to share some code, wich can simplify usage of INotifyPropertyChanged in XAML.

Take a loot at following fragments:

Snippet

namespace NotifyPropertyChanged
{
    public class BindableBase : INotifyPropertyChanged 
    {
        public event PropertyChangedEventHandler PropertyChanged;
 
        public void RaisePropertyChanged(string propertyName)
        {
            if(PropertyChanged != null)
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}

 

Class BindableBase now can become as base class for those objects, which need to notify UI about changes in source code. Consider next example:

 

Snippet

namespace NotifyPropertyChanged
{
    public class Student : BindableBase
    {
        string _firstName;
        public string FirstName
        {
            get
            {
                return _firstName;
            }
            set
            {
                if(value != _firstName)
                {
                    _firstName = value;
                    RaisePropertyChanged(nameof(FirstName));
                }
            }
        }
 
        string _lastName;
 
        public string LastName { get { return _lastName; }
            set
            {
                if(value != _lastName)
                {
                    _lastName = value;
                    RaisePropertyChanged(nameof(LastName));
                }
            }
        }
 
    }
}

In similar ways other classes that need notification about changes in their fields can implement notifications without need of writing similar code over and over again. Just don't forget for usage of DataBinding write something like this:

Snippet

public partial class MainWindow : Window
{
    Student st = new Student();
 
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = st;
    }
 
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        st.LastName = "Ivanov";
    }
}

 

Binding modes in XAML

Hello everybody,

today I want to write few words about binding in XAML.

So there are four ways of binding in XAML.

  1. One way
  2. Two way
  3. One way to source
  4. One time

 

If to speak about them more, then here it goes additional definitions:

  • OneWay : source property updates target property
  • TwoWay: source or target updates the other
  • OneWayToSource: source is updated when target is updated
  • OneTime: executed only once for initialization of target property

What is important to notice for notification of UI, there is a need for implementing INotifyPropertyChanged interface. Without it changes in source properties will not become propogated to target.

Basic elements in XAML

Hello everybody,

today I want to write few words about basic elements in XAML.

 

DependencyObject

This class is next in inheritance level in XAML after object. DependencyObject gives needed properties for representing something that participates in dependency property system. Mostly needed for data binding. It is basic for UIElement, Geometry, FameworkTemplates, Tile, ResourceDictionary.

UIElement

This is base class for all objects that have visual representation. It supports basic manipulation, appereance, layout. Also it has code for responses for user input, routed events, some aspects of animation system.

FrameworkElement

This class takes part in layout and obliged to have display space in app UI. Following properties are included: Height, Width, Alignment, Margin. Adds cooperation with DataContext.