WPF Binding Window/UserControl to Self

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

File has been tested

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

  • .NET Core 2.0, Angular 4 and MySQL. Delete Actions

    .NET Core 2.0, Angular 4 and MySQL. Delete Actions

    View: 342    Download: 0   Comment: 0

    Category: ASP.NET Framework     Fields: Other

    To finalize the coding part of the series, we are going to implement the logic for deleting the entity. To do that, we are going to create a form to support this action and send the DELETE request towards our server.

  • WPF Binding Window/UserControl to Self

    WPF Binding Window/UserControl to Self

    View: 98    Download: 0   Comment: 0

    Category: Web Development     Fields: Other

    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...

  • Workflow Manager using Unity config File in ASP.NET

    Workflow Manager using Unity config File in ASP.NET

    View: 485    Download: 0   Comment: 0

    Category: ASP.NET Framework     Fields: Other

    This article explains a design approach that involves setting the work flow of the process using config file and Unity Framework. It helps to change the flow only by updating the config file and no other code change is required.

  • SimpleXML is a PHP extension that allows us to easily manipulate and get XML data

    SimpleXML is a PHP extension that allows us to easily manipulate and get XML data

    View: 235    Download: 0   Comment: 0

    Category: Php&mySql     Fields: none

    SimpleXML provides an easy way of getting an element's name, attributes and textual content if you know the XML document's structure or layout. SimpleXML turns an XML document into a data structure you can iterate through like a collection of arrays...

  • ASP.NET Core 2.0 User Role Base Dynamic Menu Management Using Dependency Injection

    ASP.NET Core 2.0 User Role Base Dynamic Menu Management Using Dependency Injection

    View: 95    Download: 0   Comment: 0

    Category: ASP.NET Framework     Fields: Other

    In this article we will see in detail how to display role-based dynamic menu after a user logs in. For this we will create a Menu Master table and insert a few records to display the menu and link the URL to the menu based on the logged in user's...

  • A beginner's guide to URL rewriting, with plenty of examples.

    A beginner's guide to URL rewriting, with plenty of examples.

    View: 84    Download: 0   Comment: 0

    Category: Web Development     Fields: none

    URL rewriting can be one of the best and quickest ways to improve the usability and search friendliness of your site. It can also be the source of near-unending misery and suffering. Definitely worth playing carefully with it - lots of testing is...

  • How to use the PHP download file script? Class Download PHP

    How to use the PHP download file script? Class Download PHP

    View: 148    Download: 1   Comment: 0

    Category: Php&mySql     Fields: Other

    This is my favorite PHP download script. I’ve used a different more simple method until a client wanted to be able to allow their site visitors to download a large file from a password protected directory. The PHP script works on Apache web servers...

  • Data Scraping from Image using Tesseract

    Data Scraping from Image using Tesseract

    View: 68    Download: 0   Comment: 0

    Category: Web Development     Fields: Other

    Data Science is a growing field. According to CRISP DM model and other Data Mining models, we need to collect data before mining out knowledge and conduct predictive analysis. Data Collection can involve data scraping, which includes web scraping...

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