WPF: What distinguishes a dependency property from a regular CLR property?

In WPF, what does the “dependency property” really mean?

I read the Microsoft Dependency Properties Overview , but for me it really doesn't dive. Part of this article says:

Styles and patterns are the two main motivating scenarios for using dependency properties. Styles are especially useful for setting properties that define the user interface of an application (UI). Styles are usually defined as resources in XAML. Styles interact with the property system because they usually contain “setters” for certain properties, as well as “triggers” that change the value of a property based on the real-time value for another property.

And here is a sample code:

<Style x:Key="GreenButtonStyle">
  <Setter Property="Control.Background" Value="Green"/>
</Style>
....
<Button Style="{StaticResource GreenButtonStyle}">I am green!</Button>

, . , , Style , Background ? ?

+5
4

WPF, , , " "?

, DependencyProperty, , . CLR.

-. . , . - , , .

, , , "" , WPF.

, DataContext. , DataContext UserControl. "" DataContext proeprty , . CLR DataContext , .

+8

, , , - . , , , , , .

- -, DependencyObject. ( ) CLR, . . .

:

Document RichTextBox. CLR. , , , CLR. "" RichTextBox . :

public FlowDocument Document
{
   get { return (FlowDocument)GetValue(DocumentProperty); }
   set { SetValue(DocumentProperty, value); }
}

Document, SetValue DocumentProperty. ? GetValue ? ... ?

. , RichTextBox DocumentProperty. , :

public static DependencyProperty DocumentProperty = DependencyProperty.Register(
    "Document",
    typeof(FlowDocument), 
    typeof(RichTextBox));

Register , RichTextBox - , - Document FlowDocument. ... -. , , , .

Document SetValue, SetValue DocumentProperty, , RichTextBox value , ... -. DependencyObject , . , , , Dictionary<DependencyProperty, object> DependencyObject, (, RichTextBox) , GetValue SetValue . , , .

, " ", RichTextBox, .

:

  • CLR , . , . GetValue SetValue, , , , , .
  • , -, , , . ( , -, . .)
  • -, , . .

? .

. . , Binding , SetValue .

. , SetValue , . , : , , .

.. XAML, Background={DynamicResource MyBackground}, MyBackground, . ; SetValue.

. , . , SetValue, .

.. , , SetValue , .

. , , . GetValue , GetValue , . , , .

FontFamily Window ( ), . , , , - FontFamily ( ), FontFamily - (- , DependencyObject).

+27

, .

, , , - , , WPF.

1000 Label Label, Foreground, Background, FontFamily, FontSize, FontWeight .., .

, . ( )

.

// Lets register the Dependency Property with a default value of 20.5
public static readonly DependencyProperty ColumnWidthProperty =
    DependencyProperty.Register("ColumnWidth", typeof(double), typeof(MyWPFControl), new UIPropertyMetadata(20.5, ColWitdhPropChanged));

public double ColumnWidth
{
  get { return (double)GetValue(ColumnWidthProperty); }
  set { SetValue(ColumnWidthProperty, value); }
}

. , . GetValue , , Label .

SetValue, , , , , GetValue.

, WPF, . .

+9

/ . . / , CLR .

<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"
        >
    <StackPanel DataContext="{Binding ElementName=MainForm}">
        <!-- Bind to Dependency Property -->
        <Label Name="txtCount1" FontWeight="Bold" FontSize="20" Content="{Binding ElementName=MainForm, Path=Count1, Mode=OneWay}" />

        <!-- Bind to CLR Property -->
        <Label Name="txtCount2" Content="{Binding ElementName=MainForm, Path=Count2, Mode=OneWay}"></Label>

        <!-- Bind to Dependency Property (Using DataContext declared in StackPanel) -->
        <Label Name="txtCount3" FontWeight="Bold" FontSize="20" Content="{Binding Count1}" />

        <!-- Child Control binding to Dependency Property (Which propagates down element tree) -->
        <local:UserControl1 />

        <!-- Child Control binding to CLR Property (Won't work as CLR properties don't propagate down element tree) -->
        <local:UserControl2 />

        <TextBox Text="{Binding ElementName=txtCount1, Path=Content}" ></TextBox>
        <TextBox Text="{Binding ElementName=txtCount2, Path=Content}" ></TextBox>

        <Button Name="btnButton1" Click="btnButton1_Click_1">Increment1</Button>
        <Button Name="btnButton2" Click="btnButton1_Click_2">Increment2</Button>
    </StackPanel>
</Window>

<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">
    <StackPanel>
        <Label Content="{Binding Count1}" ></Label>
        <!--
        <Label Content="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=Count1}"></Label>
        -->
    </StackPanel>
</UserControl>

<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">
    <StackPanel>
        <Label Content="{Binding Count2}" ></Label>
        <!--
        <Label Content="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=Count2}"></Label>
        -->
    </StackPanel>
</UserControl>

( CLR Dependency):

    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
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        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++;
            }
        }
    }

, , - - , , . , http://en.csharp-online.net, FontSize FontStyle, "Window", :

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  Title="About WPF Unleashed" SizeToContent="WidthAndHeight"
  FontSize="30" FontStyle="Italic"
  Background="OrangeRed">
  <StackPanel>
    <Label FontWeight="Bold" FontSize="20" Foreground="White">
      WPF Unleashed (Version 3.0)
    </Label>
    <Label>© 2006 SAMS Publishing</Label>
    <Label>Installed Chapters:</Label>
    <ListBox>
      <ListBoxItem>Chapter 1</ListBoxItem>
      <ListBoxItem>Chapter 2</ListBoxItem>
    </ListBox>
    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
      <Button MinWidth="75" Margin="10">Help</Button>
      <Button MinWidth="75" Margin="10">OK</Button>
    </StackPanel>
    <StatusBar>You have successfully registered this product.</StatusBar>
  </StackPanel>
</Window>

: http://www.codeproject.com/Articles/29054/WPF-Data-Binding-Part-1 http://en.csharp-online.net/WPF_Concepts%E2%80%94Property_Value_Inheritance

0
source

All Articles