Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A simple/fundamental difference - Change Notification: Changes to Dependency Properties are reflected/refreshed in UI on changes whereas CLR properties don't. </p> <pre><code>&lt;Window x:Class="SampleWPF.MainWindow" x:Name="MainForm" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:SampleWPF" Title="About WPF Unleashed" SizeToContent="WidthAndHeight" Background="OrangeRed" &gt; &lt;StackPanel DataContext="{Binding ElementName=MainForm}"&gt; &lt;!-- Bind to Dependency Property --&gt; &lt;Label Name="txtCount1" FontWeight="Bold" FontSize="20" Content="{Binding ElementName=MainForm, Path=Count1, Mode=OneWay}" /&gt; &lt;!-- Bind to CLR Property --&gt; &lt;Label Name="txtCount2" Content="{Binding ElementName=MainForm, Path=Count2, Mode=OneWay}"&gt;&lt;/Label&gt; &lt;!-- Bind to Dependency Property (Using DataContext declared in StackPanel) --&gt; &lt;Label Name="txtCount3" FontWeight="Bold" FontSize="20" Content="{Binding Count1}" /&gt; &lt;!-- Child Control binding to Dependency Property (Which propagates down element tree) --&gt; &lt;local:UserControl1 /&gt; &lt;!-- Child Control binding to CLR Property (Won't work as CLR properties don't propagate down element tree) --&gt; &lt;local:UserControl2 /&gt; &lt;TextBox Text="{Binding ElementName=txtCount1, Path=Content}" &gt;&lt;/TextBox&gt; &lt;TextBox Text="{Binding ElementName=txtCount2, Path=Content}" &gt;&lt;/TextBox&gt; &lt;Button Name="btnButton1" Click="btnButton1_Click_1"&gt;Increment1&lt;/Button&gt; &lt;Button Name="btnButton2" Click="btnButton1_Click_2"&gt;Increment2&lt;/Button&gt; &lt;/StackPanel&gt; &lt;/Window&gt; &lt;UserControl x:Class="SampleWPF.UserControl1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"&gt; &lt;StackPanel&gt; &lt;Label Content="{Binding Count1}" &gt;&lt;/Label&gt; &lt;!-- &lt;Label Content="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=Count1}"&gt;&lt;/Label&gt; --&gt; &lt;/StackPanel&gt; &lt;/UserControl&gt; &lt;UserControl x:Class="SampleWPF.UserControl2" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"&gt; &lt;StackPanel&gt; &lt;Label Content="{Binding Count2}" &gt;&lt;/Label&gt; &lt;!-- &lt;Label Content="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=Count2}"&gt;&lt;/Label&gt; --&gt; &lt;/StackPanel&gt; &lt;/UserControl&gt; </code></pre> <p>And the code behind here (To declare the CLR and Dependency property):</p> <pre><code> using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace SampleWPF { /// &lt;summary&gt; /// Interaction logic for MainWindow.xaml /// &lt;/summary&gt; public partial class MainWindow : Window { public static readonly DependencyProperty Count1Property; private int _Count2 = 2; public int Count2 { get { return _Count2; } set { _Count2 = value; } } public MainWindow() { return; } static MainWindow() { // Register the property MainWindow.Count1Property = DependencyProperty.Register("Count1", typeof(int), typeof(MainWindow), new FrameworkPropertyMetadata(1, new PropertyChangedCallback(OnCount1Changed))); } // A .NET property wrapper (optional) public int Count1 { get { return (int)GetValue(MainWindow.Count1Property); } set { SetValue(MainWindow.Count1Property, value); } } // A property changed callback (optional) private static void OnCount1Changed( DependencyObject o, DependencyPropertyChangedEventArgs e) { } private void btnButton1_Click_1(object sender, RoutedEventArgs e) { Count1++; } private void btnButton1_Click_2(object sender, RoutedEventArgs e) { Count2++; } } } </code></pre> <p>Another feature provided by Dependency Properties is value inheritance - value set in top level elements propagates down the element tree - In following example taken from <a href="http://en.csharp-online.net" rel="nofollow">http://en.csharp-online.net</a>, FontSize and FontStyle declared on "Window" tag is applied to all child elements underneath:</p> <pre><code>&lt;Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" Title="About WPF Unleashed" SizeToContent="WidthAndHeight" FontSize="30" FontStyle="Italic" Background="OrangeRed"&gt; &lt;StackPanel&gt; &lt;Label FontWeight="Bold" FontSize="20" Foreground="White"&gt; WPF Unleashed (Version 3.0) &lt;/Label&gt; &lt;Label&gt;© 2006 SAMS Publishing&lt;/Label&gt; &lt;Label&gt;Installed Chapters:&lt;/Label&gt; &lt;ListBox&gt; &lt;ListBoxItem&gt;Chapter 1&lt;/ListBoxItem&gt; &lt;ListBoxItem&gt;Chapter 2&lt;/ListBoxItem&gt; &lt;/ListBox&gt; &lt;StackPanel Orientation="Horizontal" HorizontalAlignment="Center"&gt; &lt;Button MinWidth="75" Margin="10"&gt;Help&lt;/Button&gt; &lt;Button MinWidth="75" Margin="10"&gt;OK&lt;/Button&gt; &lt;/StackPanel&gt; &lt;StatusBar&gt;You have successfully registered this product.&lt;/StatusBar&gt; &lt;/StackPanel&gt; &lt;/Window&gt; </code></pre> <p><strong>References:</strong> <a href="http://www.codeproject.com/Articles/29054/WPF-Data-Binding-Part-1" rel="nofollow">http://www.codeproject.com/Articles/29054/WPF-Data-Binding-Part-1</a> http://en.csharp-online.net/WPF_Concepts%E2%80%94Property_Value_Inheritance</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload