What is Dependencyproperty and Dependency object

Dependency property can be linked to another property such that a change in the value of one changes the other.

The purpose of dependency properties is to provide a way to compute the value of a property based on the value of other inputs. These other inputs might include system properties such as themes and user preference, just-in-time property determination mechanisms such as data binding and animations/storyboards, multiple-use templates such as resources and styles, or values known through parent-child relationships with other elements in the element tree.

CLR property vs Dependency property: Continue reading

Difference between DataContext and ItemsSource property in Silverlight/WPF.

  • DataContext expects an object type where ItemsSource expects IEnumerable type objects.
  • DataContext is a dependency property is exposed by FrameworkElement base class,where as ItemsSource is defined by the ItemsControl class. All the descendants of FrameworkElement can utilize the DataContext property and set an object to its value. But we can only set a type of IEnumerable(or instance of class that derives from). 
  • DataContext does not generate template, it only used to hold common data for other controls to bind. In terms of ItemsSource property, it is mainly used to generate template regardless of you set it in XAML or in the code behind. 
  • DataContext is mainly used to hold common data that other child want to share. Thus it can be inherited by other child elements without problem. But for ItemsSource, it not used to share data in the visual tree. It is only valid for the element that defined. There is still one thing to be noted is that the child element can override the DataContext of the perent DataContext no mater directly or indirectly.

Continue reading

Is there a way to use WPF without XAML

You can write WPF apps w/o XAML. Everything you can achieve in xaml, you can achieve in C# code is really true but not the opposite.  But xaml is very handy to define styles and templates, or initialize tons of objects. It is much easier to read than c# code. And it is easy to maintain.

XAML is also mustly used in WPF UI bacause it makes it possible to have a dedicated designer (not programmer) to design the UI.

How to get this code going:

  • Create a Windows Presentation Foundation project named HelloWorldManual
  • Remove the App.xaml and Window1.xaml from the project
  • Add a new class file named MyWindow.cs and paste this into it:

Continue reading

By Sriramjithendra Posted in WPF, WPF

Change disabled background color(to GRAY) of TextBox in WPF

 <SolidColorBrush x:Key="DisabledForegroundBrush" Color="Red" />
 <SolidColorBrush x:Key="DisabledBackgroundBrush" Color="White" />
 <Style TargetType="{x:Type TextBox}">
 <Trigger Property="IsEnabled" Value="False">
 <Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}" />
 <Setter Property="Background" Value="{StaticResource DisabledBackgroundBrush}" />

If You need to apply it for all the textbox controls, use the above code. For specific textbox, just set the key and apply the style to that Textbox.


By Sriramjithendra Posted in WPF

INotifyPropertyChanged- WPF

A very common and popular way to synchronize data between the model and the view in WPF is using DataBinding. The value of the model is transferred to the view once, when the binding is initialized. But for every subsequent change, the model must notify the binding to transfer the value again. This is done by implementing the INotifyPropertyChangedinterface on the model.

In the setter of every bound property there must be some code to raise the PropertyChanged event, passing the name of the modified property as a string. This has some disadvantages:

  • You cannot use Auto-properties
  • The property name is passed as a string. This brings ugly errors, when you forgot to change the string after a rename.
  • Every property needs extra code


RelativeSources in WPF bindings

The RelativeSource is a markup extension that is used in particular binding cases when we try to bind a property of a given object to another property of the object itself, when we try to bind a property of a object to another one of its relative parents, when binding a dependency property value to a piece of XAML in case of custom control development and finally in case of using a differential of a series of a bound data. All of those situations are expressed as relative source modes. I will expose all of those cases one by one. Continue reading

Attached property – WPF. Also diff b/w attached and Dependdency prop

An attached property is intended to be used as a type of global property that is settable on any object. Attached properties are a special form of dependency property.  They exist so that a child element can store a value associated with a property defined on an ancestor element.  This is commonly used in the interaction between elements and the WPF layout infrastructure,. Continue reading