Dependency property in WPF

9 篇文章 0 订阅

/*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


<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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值