Dependency property in WPF
2012-11-19 05:45
267 查看
/*by Jiangong SUN*/
XAML: eXtensible Application Markup Language
Dependency property represents a property that can be set through methods such as, styling, data binding, animation, and inheritance.
Dependency properties are the workhorse of WPF. This infrastructure provides for many of WPF's features, such as data binding, animations, and visual inheritance. In fact, most of the various element properties are Dependency Properties.
Here is main window: MainWindow.xaml
Code behind : MainWindow.xaml.cs
SimpleControl.xaml.cs
reference:
http://www.wpftutorial.net/dependencyproperties.html
XAML: eXtensible Application Markup Language
Dependency property represents a property that can be set through methods such as, styling, data binding, animation, and inheritance.
Dependency properties are the workhorse of WPF. This infrastructure provides for many of WPF's features, such as data binding, animations, and visual inheritance. In fact, most of the various element properties are Dependency Properties.
Here is main window: MainWindow.xaml
<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfApplication1" <!--local application namespace--> Title="MainWindow" Height="350" Width="525"> <StackPanel> <local:SimpleControl x:Name="_simple" /> <!--use a wpf user control named "SimpleControl"--> <TextBlock Text="{Binding YearPublished, ElementName=_simple}" FontSize="30" /> <!--YearPublished property is binding--> <Button Content="Change Value" FontSize="20" Click="OnChangeValue" /> <!--When user click on the button, it will call the event handler "OnChangeValue"--> </StackPanel> </Window>
Code behind : MainWindow.xaml.cs
namespace WpfApplication1 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void OnChangeValue(object sender, RoutedEventArgs e) { _simple.YearPublished++; //property YearPublished's value increment by 1 in user control SimpleControl's instance } } }
SimpleControl.xaml.cs
namespace WpfApplication1 { /// <summary> /// Interaction logic for SimpleControl.xaml /// </summary> public partial class SimpleControl : UserControl { public SimpleControl() { InitializeComponent(); } //propdp public int YearPublished { get { return (int)GetValue(YearPublishedProperty); } set { SetValue(YearPublishedProperty, value); } } // Using a DependencyProperty as the backing store for YearPublished. This enables animation, styling, binding, etc... public static readonly DependencyProperty YearPublishedProperty = DependencyProperty.Register("YearPublished", typeof(int), typeof(SimpleControl), new UIPropertyMetadata(2000, OnValueChanged)); private static void OnValueChanged(DependencyObject obj,DependencyPropertyChangedEventArgs e) { // do something when property changes } } }
reference:
http://www.wpftutorial.net/dependencyproperties.html
相关文章推荐
- WPF Background property does not point to a dependencyobject in path '(0).(1)'
- WPF Unleashed Chapter 3:Important New Concepts in WPF ---Dependency Properties(Property Value Inheritance)
- WPF里的DependencyProperty(5)
- WPF中监视DependencyProperty的变化
- [WPF/Silverlight]简单说下CLR Property与WPF Dependency Property的区别
- WPF 依赖属性,用户控件依赖属性(DependencyProperty 依赖属性、GetValue() SetValue() CLR属性包装器、SetBinding 数据绑定)
- (转)WPF Custom Control Dependency Property Gotcha
- WPF Unleashed Chapter 3:Important New Concepts in WPF ---Dependency Properties (Support for Multiple Providers)
- WPF DependencyProperty
- WPF 错误:DependencyProperty.UnsetValue}' is not a valid value for property 'Background'
- .NET: WPF DependencyProperty
- Example on implementing DependencyProperty in User control
- Property System in WPF
- WF,WPF,Silverlight的DependencyProperty 附加属性
- WPF里的DependencyProperty(5)
- WPF中的依赖属性(Dependency Property)
- WPF里的DependencyProperty(3)
- WPF里的DependencyProperty(3)
- Silverlight&WPF依赖属性DependencyProperty讲解
- WPF学习笔记8: 依赖属性(Dependency Property)