WPF series -- controls add dependency properties, wpf controls

Source: Internet
Author: User

WPF series -- controls add dependency properties, wpf controls
Concept, purpose, and how to create and use dependency attributes. This article uses a custom TimePicker control to demonstrate a simple application of WPF dependency attributes.

Start with one of TimePicker.

Public int MyProperty {get {return (int) GetValue (MyPropertyProperty);} set {SetValue (MyPropertyProperty, value) ;}// Using a DependencyProperty as the backing store for MyProperty. this enables animation, styling, binding, etc... public static readonly DependencyProperty MyPropertyProperty = DependencyProperty. register ("MyProperty", typeof (int), typeof (ownerclass), new PropertyMetadata (0 ));

Usage: here we use binding. We will introduce the Time attribute of the custom control later.

How to Use dependency properties to build a custom control with bound TimePicker

Create a new WPF project named DependencyPropertyDemo <Grid> <Grid. columnDefinitions> <ColumnDefinition Width = "9 *"/> <ColumnDefinition Width = "24"/> <ColumnDefinition Width = "10 *"/> </Grid. columnDefinitions> <ComboBox Name = "cbbHour" Grid. column = "0"/> <Label Content = ":" Grid. column = "1"/> <ComboBox Name = "cbbMinute" Grid. column = "2"/> </Grid>

BehindCode creates a dependency attribute named Time, as follows:

public string Time        {            get { return (string)GetValue(TimeProperty); }            set { SetValue(TimeProperty, value); }        }        public static readonly DependencyProperty TimeProperty =            DependencyProperty.Register("Time",                typeof(string),                typeof(TimePicker),                new PropertyMetadata(defaultValue: "00:00",                     propertyChangedCallback: null,                    coerceValueCallback: coerceValueCallback));        private static object coerceValueCallback(DependencyObject d, object baseValue)        {            if (baseValue != null)            {                var control = d as TimePicker;                var times = baseValue.ToString().Split(':');                control.cbbHour.SelectedItem = times[0];                control.cbbMinute.SelectedItem = times[1];                return baseValue.ToString();            }            return baseValue;        }

Here we will introduce in detail the three parameters of PropertyMetadata: defaultValue: Default Value, no need to introduce; propertyChangedCallback: Notification event after attribute change. No notification is required here, So null value is passed in; coerceValueCallback: this is a notification event when the new value of the property value is bound. Here, the obtained value is used to assign a value to the control. By now, the Time dependency attribute is half done.

The TimePicker attribute depends on the binding of the Time attribute.

The binding method is simple, for example. If Time is a common. net attribute rather than a dependency attribute, the binding method cannot be used in this way.

<local:TimePicker HorizontalAlignment="Left" Margin="137,38,0,0" VerticalAlignment="Top" Width="161"                          Time="{Binding StartTime,Mode=TwoWay}"/>

If you add a common attribute of Code in the TimePicker background and bind it, the following conditions will occur.

Summary

There are certain restrictions on the use of dependency properties, that is, the Class Object registered by him, that is, the previous ownerclass must inherit the base class DependencyObject, but don't worry, most elements of WPF indirectly integrate this base class.

The TimePicker control implemented in this article can solve the problem of No Time Selection Control in WPF in some cases.

Download Demo

If you believe that reading this blog has some gains, please click the [recommendation] and [follow] buttons below. Thank you for your support.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.