WPF property system
Dependency Properties and CLR Properties
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.
Dependency Properties Back CLR Properties
public static readonly DependencyProperty IsSpinningProperty =
DependencyProperty.Register(
"IsSpinning", typeof(Boolean),
...
);
public bool IsSpinning
{
get { return (bool)GetValue(IsSpinningProperty); }
set { SetValue(IsSpinningProperty, value); }
}
The IsSpinningProperty is the DependencyProperty identifier used to incorporate the WPF properties system. The IsSpinning is the CLR "wrapper" for the underlying dependency property.
Sample
// Create the application's main window mainWindow = new Window (); mainWindow.Title = "DockPanel Sample"; // Create the DockPanel DockPanel myDockPanel = new DockPanel(); myDockPanel.LastChildFill = true; // Define the child content Border myBorder1 = new Border(); myBorder1.Height = 25; myBorder1.Background = Brushes.SkyBlue; myBorder1.BorderBrush = Brushes.Black; myBorder1.BorderThickness = new Thickness(1); *DockPanel.SetDock(myBorder1, Dock.Top);* TextBlock myTextBlock1 = new TextBlock(); myTextBlock1.Foreground = Brushes.Black; myTextBlock1.Text = "Dock = Top"; myBorder1.Child = myTextBlock1;
DockPanel.SetDock
public static void SetDock(UIElement element, Dock dock)
{
if (element == null)
{
throw new ArgumentNullException("element");
}
element.SetValue(DockProperty, dock);
}
System.Windows.DependencyObject.SetValue
public void SetValue(DependencyProperty dp, object value) { base.VerifyAccess(); PropertyMetadata metadata = this.SetupPropertyChange(dp); this.SetValueCommon(dp, value, metadata, false, false, OperationType.Unknown, false); }
System.Windows.DependencyObject.SetValueCommon
some code intend here but it is missing...
In this way, the DockPanel can dynamically attach some data into the UI elemeent, and can retrieve them back during its layout.
Practice
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; namespace DPChangeSample { class DPChangeSample : DependencyObject { public static DependencyProperty AProperty = DependencyProperty.Register("A", typeof(int), typeof(DPChangeSample), new PropertyMetadata(propertyChanged)); public static DependencyProperty BProperty = DependencyProperty.Register("B", typeof(int), typeof(DPChangeSample), new PropertyMetadata(propertyChanged)); public static DependencyProperty CProperty = DependencyProperty.Register("C", typeof(int), typeof(DPChangeSample), new PropertyMetadata(propertyChanged)); private static void propertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { ((DPChangeSample)d).recalculate(); } private void recalculate() { // Using A, B, C do some cpu intensive calculations } public DPChangeSample(int a, int b, int c) { SetValue(AProperty, a); SetValue(BProperty, b); SetValue(CProperty, c); } static void Main(string[] args) { var dpSample = new DPChangeSample(1, 2, 3); } } }
Post by: Jalen Wang (转载请注明出处)
posted on 2013-01-09 16:53 Jalen Wang 阅读(176) 评论(0) 编辑 收藏 举报