When designing and developing a WPF custom control, we often add some dependency properties for the control to make it easier to bind or animate. In fact, in addition to being able to add a normal dependency property, we can add a read-only dependency property (collectively referred to as "read-only property") to the control to increase the flexibility of the control.
It sounds a bit contradictory. Read-only dependency properties that can only be read and not written, but how can you improve the flexibility of the control? Think of the IsMouseOver properties we used to understand that they are read-only properties, but it would be difficult to control the style without them.
So, in summary, the characteristics of a read-only property are: cannot be assigned, cannot be bound, cannot be used for animation, cannot be verified, etc., and the main purpose of this is to combine the property trigger (Trigger) to implement a style switch .
Practice
For example, we want to create a Filepicker control that allows the user to select a file. Then, it includes at least one TextBlock (or TextBox) and one Button, respectively, to display the path to the selected file and open the dialog box.
Now we want to implement the control renders a style when the user selects the file. To do this, we can add a isfilepicked read-only property, and in the ControlTemplate, we added Trigger to control the style changes.
1. Create (define and register)
The
Creates a read-only property just like creating a normal dependency property, including the three steps of defining, registering, and wrapping with CLR properties. The difference is to register using DependencyProperty's Registerreadonly method, which returns the DependencyPropertyKey object, which contains the identifier for the read-only property, which is the read-only property associated with it. and assigning a value to a read-only property is also through it (note: The read-only property itself cannot be assigned), the code is as follows:
// definition and Registration of Read-only properties Private Static DependencyPropertyKey Isfilepickedpropertykey = dependencyproperty.registerreadonly ("IsFilePicked "typeof(booltypeofnew PropertyMetadata (false )); Public Static DependencyProperty isfilepickedproperty = Isfilepickedpropertykey.dependencyproperty;
Notice the naming, because the property we are creating is isfilepicked, so the above two variables are added after this name with a suffix, respectively, PropertyKey and properties, which is the naming convention.
2. Packaging
It then wraps it as a CLR property, because this is a read-only property, so you just need a get segment, and the code looks like this:
// Wrapper for Read -only properties Public BOOL isfilepicked { getreturn (bool) GetValue (Isfilepickedproperty);} }
3. Assigning values via DependencyPropertyKey
In the appropriate location (after the user has selected a file), using the SetValue method to assign a value, SetValue has two overloads, to assign a value to a read-only property, use the second SetValue (DependencyPropertyKey key, Object value) , the code is as follows:
true);
4. Application
Once the logic is written, add the following XAML code to the template:
<controltemplate.triggers> <Trigger Property= "isfilepicked"Value= "True"> <!--Show Green Border - <Setter Property= "BorderBrush"Value= "Green" /> <Setter Property= "BorderThickness"Value= "2" /> </Trigger> </controltemplate.triggers>
Summarize
This article briefly describes how to create and use read-only dependency properties in WPF to use it appropriately, giving us more flexibility to control the style of a custom control.
WPF: Introduction and practice of Read-only dependency properties