Loading...

WPF Binding Window/UserControl to Self

View: 167    Dowload: 0   Comment: 0   Post by: admin   Category: Web Development   Fields: Other

Download  wpf-binding-window-usercontrol.zip (18.29 KB)

You need to Sign In to download the file wpf-binding-window-usercontrol.zip
If you do not have an account then Sign up for free here

Introduction

I recently started a project where in order to bind to properties within the Code section of UserControl (or Window), they were using DependencyProperty definitions, in fact sometimes it was even worse, they were also deriving from INotifyPropertyChanged, and using the OnPropertyChanged.

Many people consider code within the Code section of a XAML document bad, and that only in rare instances will I put code into the Code section, prefering to create a behavior, which can be reused.

There are times when it is necessary to use DependencyProperty definitions, but seldom in a UserControl.

Difference between DependencyProperty and INotifyPropertyChanged

The following was published by "LBugnion":

INotifyPropertyChanged has advantages over DependencyProperty definitions:

  • This is more lightweight
  • Allows you more freedom in modeling your objects
  • Can be serialized easily
  • You can raise the event when you want, which can be useful in certain scenarios, for example when you want to bundle multiple changes in only one UI operation, or when you need to raise the event even if the data didn't change (to force redraw...)

DependencyProperty has the following advantages:

  • Callback mechanism (almost) for free, allowing notification when the property value changes
  • Coercion mechanism allows you to define rules for max, min and present value of the property.
  • It is faster: Optimizing Performance: Data Binding

There are apparently other considerations. The consensus is that DependencyProperty definitions are great for controls (and you can implement a CustomControl), but for data objects you should implement INotifyPropertyChanged.

Doing the Binding to the Window/UserControl

I saw the following when looking at the code in the project:

public UserInputControl()
{
    InitializeComponent();
    LayoutRoot.DataContext = this;
}

Although this is fine, I really prefer to do this in XAML.

You can actually do the setting of the DataContext in XAML, if there is a separate class that is associated with DataContext for a XAML file, I will do the following:

<Window.DataContext>
    <local:ViewModel/>
</Window.DataContext>

Normally I will use this method of associating a ViewModel with a View only for sample projects. The nice thing about this technique is that it creates an instance of the VeiwModel and automatically assignes it to the DataContext of the View.

Maybe you think you could use this method for associating the DataContext with the class itself. NO!!! When you try to use the XAML above for the class itself, a new instance of the XAML and class is created, and that creates a new instance of the XAML and class again. This happens recursively so you run out of memory. 

The following XAML is one way to associate the DataContext with the class itself:

<Window x:Class="DataContextToSelfSample.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

        xmlns:local="clr-namespace:DataContextToSelfSample"

        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

        DataContext="{Binding RelativeSource={RelativeSource Self}}"

        Title="MainWindow"

        Width="400"

        Height="300"

        mc:Ignorable="d">

What is happening is that the RelativeSource of the Binding is being used with the RelativeSource as Self.

WPF Binding Window/UserControl to Self

I recently started a project where in order to bind to properties within the Code section of UserControl (or Window), they were using DependencyProperty definitions, in fact sometimes it was even worse, they were also deriving from INotifyPropertyChanged, and using the OnPropertyChanged.

Posted on 11-04-2018 

Comment:

To comment you must be logged in members.

Files with category

  • Twitter-like Hashtag Function in PHP

    Twitter-like Hashtag Function in PHP

    View: 0    Download: 0   Comment: 0

    Category: Php&mySql     Fields: none

    If you ever want a function to hashtag and style words within a string which has '#' next to it like twitter. This piece of code will help

  • Mini Youtube Using ReactJS

    Mini Youtube Using ReactJS

    View: 25    Download: 2   Comment: 0

    Category: Javascript     Fields: none

    This is one the best starter for ReactJS. MiniYoutube as the name suggests is a youtube like website developed using reactJS and youtube API. This project actually let's you search , play and list youtube videos. Do check it out and start learning...

  • PSITS Automated Voting System

    PSITS Automated Voting System

    View: 24    Download: 3   Comment: 0

    Category: Php&mySql     Fields: none

    A free sourcecode for PSITS Automated Voting System develop in PHP programming language. The purposed of the system is to automate the process of voting and maintain the quality of data

  • Document Management System in VB.Net

    Document Management System in VB.Net

    View: 24    Download: 1   Comment: 0

    Category: Forum PHPBB, VBB     Fields: none

    This is a Client Server project entitled Document Management System written in Vb.net and SQL Server Management Studio R2 2008 database for Mines and Geosciences Bureau . There are three (3) sections in the Mines and Geosciences Bureau, namely: under...

  • PHP-University Application System

    PHP-University Application System

    View: 30    Download: 6   Comment: 0

    Category: Php&mySql     Fields: none

    Greeting from Malawi the warm heart of Africa. I developed this university registration system project just to share with the people who can make use of the project like this and at the same time for others like students who can learn from it. This...

  • School Event Management System in PHP/MSQLi

    School Event Management System in PHP/MSQLi

    View: 22    Download: 0   Comment: 0

    Category: Php&mySql     Fields: none

    This School Event Management System can create school events such as Volleyball games, Basketball, Cultural presentation, Election of school officers etc . During school election Instead of having a compile list of candidates and voters this system...

  • Resort Reservation System with PayPal/Credit Card/Debit Card Payment

    Resort Reservation System with PayPal/Credit Card/Debit Card Payment

    View: 27    Download: 0   Comment: 0

    Category: Php&mySql     Fields: none

    This reservation system has the ability to help its customers find available rooms, cottages and even function hall for their convenience . And in here, they will also have the idea of the room rates where they can quickly reserve for their family...

  • Activity log

    Activity log

    View: 20    Download: 0   Comment: 0

    Category: Php&mySql     Fields: none

    Simple program to track user's activity log-in time and online/offline status.

 
File suggestion for you
Loading...
File top downloads
Loading...
Loading...
Codetitle - library source code to share, download the file to the community
Copyright © 2018. All rights reserved. codetitle Develope by Vinagon .Ltd