WPF: Introduction and practice of read-only dependency attributes, wpf read-only
When designing and developing a WPF custom control, we often add some dependency attributes to the control to facilitate binding or animation. In fact, in addition to adding normal dependency properties, we can also addRead-Only dependency attribute(Hereinafter referred to as "read-only property") to increase the flexibility of the control.
This sounds a bit contradictory. The read-only dependency attribute can only be read and cannot be written, but how can it improve the flexibility of the control? Think about the commonly used IsMouseOver and other attributes. They are all read-only attributes, but it is very difficult to control the style without them.
So, to sum up,Features of read-only attributesYes: no value assignment, no binding, no animation, no verification, etc. The main purpose of using it isUse Trigger to implement style Switching.
Practice
For example, we want to create a FilePicker control through which users can select files. Then, it contains at least one TextBlock (or TextBox) and one Button, which are used to display the path of the selected file and the open dialog box respectively.
Now we want to implement the control to display a certain style after a file is selected. To do this, we can add an IsFilePicked read-only attribute and add a Trigger in the ControlTemplate to control style changes.
1. Create (define and register)
Creating a read-only property is the same as creating a common dependency property, including defining, registering, and wrapping the property with CLR. The difference is that the RegisterReadOnly method of DependencyProperty is used for registration. This method returns the DependencyPropertyKey object, which contains the identifier of the corresponding read-only attribute, that is, the read-only attribute associated with it, it is also used to assign a value to a read-only attribute (note: the read-only attribute itself cannot be assigned). The Code is as follows:
// Read-only attribute definition and Registration of private static DependencyPropertyKey IsFilePickedPropertyKey = DependencyProperty. registerReadOnly ("IsFilePicked", typeof (bool), typeof (FilePicker), new PropertyMetadata (false); public static DependencyProperty IsFilePickedProperty = IsFilePickedPropertyKey. dependencyProperty;
Note the naming rules. Because the attribute we want to create is IsFilePicked, the above two variables are added with the suffix after the name, namely PropertyKey and Property, which are naming rules.
2. Packaging
Then, wrap it as a CLR attribute. Because this is a read-only attribute, you only need to get the segment. The Code is as follows:
// Public bool IsFilePicked wrapped in read-only attributes {get {return (bool) GetValue (IsFilePickedProperty );}}3. assign values through DependencyPropertyKey
Use the SetValue method to assign values at a proper location (after the user selects a file). SetValue has two reloads. To assign values to a read-only attribute, use the second SetValue (DependencyPropertyKey key, object value), the Code is as follows:
SetValue(IsFilePickedPropertyKey, true);
4. Application
After the logic is written, add the following XAML code to the template:
<ControlTemplate. Triggers> <Trigger Property = "IsFilePicked" Value = "True"> <! -- Display a Green Border --> <Setter Property = "BorderBrush" Value = "Green"/> <Setter Property = "BorderThickness" Value = "2"/> </Trigger> </ controlTemplate. triggers>
Summary
This article briefly introduces how to create and use the read-only dependency attribute in WPF and use it appropriately, so that we can flexibly control the style of custom controls.