WPF series - control is added dependency property (turn) Series WPF - control is added dependency property

WPF series - control is added dependency property

The concept of dependency properties, uses, how to create and use. This article used a simple application TimePicker custom controls to demonstrate the dependence of the properties of WPF.

First the effect of a TimePicker of FIG.

GIF

 

 

 

 

 

 

 

 

 

Concepts and Applications: dependency property is a property .net package for conventional, so that a conventional support .net attribute data binding in WPF, animation, styles and so on.

New: arbitrary code code file, then double-click input propdp tab key. Generating the following code block.

     MyProperty: attribute name dependency; ownerclass: current dependency property binding to all classes; new PropertyMetadata initialization is dependent properties of the object, where 0 represents the default value.

Copy the code
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));
Copy the code

Use: Here we use the bindings bind, later using the Time to introduce custom attributes.

How to use the dependency property to build a custom control with binding TimePicker

Create a new project named DependencyPropertyDemo WPF project  image, create a custom control TimePicker. Xaml layout is as follows:

Copy the code
<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>
Copy the code

BehindCode a new name for Time-dependent properties, as follows:

Copy the code
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;
        }
Copy the code

Here are details about the PropertyMetadata three parameters: defaultValue: the default, not introduced; propertyChangedCallback: after property change notification events, there is no need of any notice, so pass null value; coerceValueCallback: This is a new property value value binding when, event notification, used here to get the value assigned to the control. Time here will depend on the completion of half of the property.

TimePicker properties depend on the binding properties of Time

Binding is very simple, as shown below. If Time is a common attribute of .net instead of relying on attributes, such use is not binding way.

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

Add to add a general assumption that property TimePicker Code in the background, the use of binding, then the following conditions occur.

image

 

After word summary

Dependency property use there is a certain limit, he is a registered class objects, that is, ownerclass above must inherit DependencyObject base class, but do not worry, WPF most elements are integrated indirect base class.

TimePicker controls implemented in this article can be resolved at no Time WPF selection control problems in some cases.

Demo Download

If you think you are reading this blog make some gains, please click on the following [Recommended] and [attention] button, thank you for your support, I was devastated North Korea.Wink

Guess you like

Origin www.cnblogs.com/lsgsanxiao/p/11330506.html