It has been more than a year since I wrote the last article. WPF is too big and there are too many things to write. I will still focus on custom controls to expand on the technical points related to them. Everyone is also welcome to participate in the discussion. We will discuss this article The most important thing is WPF dependency properties. Friends who have been in contact with it should have some understanding of it, but before we talk about WPF dependency properties, let’s take a look at how the properties in .net were used before WPF appeared.
Common attribute issues
c# basic properties
ASP.NET custom control properties
WinForm custom control properties
Javascript custom control properties
c# basic properties
namespace WPFControlTutorialPart2_Basic
{
public class Person
{
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
//automatic properties
public int Age { get; set; }
}
}
The above should be regarded as the most basic attributes of C#, and everyone is familiar with them.
Note: The default properties are not initialized, otherwise many unknown errors may occur.
(You can choose to read the following. If you have been exposed to the following technologies, it is actually very simple)
ASP.NET custom control properties
namespace WPFControlTutorialPart2_WebApp
{
public class WebFromControl : WebControl
{
private string _controlName;
public string ControlName
{
get
{
return _controlName;
}
set
{
_controlName = value;
}
}
protected override void RenderContents(HtmlTextWriter output)
{
output.Write("The control name is:"+ControlName);
}
}
}
The above is a simple custom web server control.
The asp.net control focuses on rendering. The RenderContents method will be called every time the page is refreshed, so that regardless of whether the properties have changed or not, they will be re-rendered. That is, there is no need for property changes to notify UI redrawing.
Note: If it is mandatory that the ControlName attribute must have a value to ensure the validity of the input result, it can be processed in the get method.
get
{
if (_controlName == null) _controlName = string.Empty;
return _controlName;
}
WinForm custom control properties
When the property changes, it is not redrawn. You must manually call the Invalidate method to enter the OnPaint method to redraw.
Note: If you want to redraw the interface, you must manually call the method
JavaScript custom control properties
<div id="demoDiv" style="background-color: Red">hello</div>
<script>
function ElementControl(elementName) {
this.element = document.getElementById(elementName);
}
ElementControl.prototype.setWidth = function(width) {
this.element.style.width = width;
}
var obj = new ElementControl("demoDiv");
obj.setWidth("hello");
</script>
JavaScript variable types do not have strong types. For example, when setting the width of dom, the type can only be an integer. If the value is passed as a string, an error will occur (of course this is artificial).
(I don’t know if people who have done js front-end controls feel this way. Defining a property and then reassigning and redrawing the control is a painful thing. It must be adjusted manually, and it will affect the entire control design.)
Note: Verify the validity of attribute assignments.
From the above examples, we can see that pure attributes cannot meet program needs.
The above requirements are necessary, occur often, and are very frequent.
Standardized solutions
The above introduces a series of problems with attributes. If there are problems, there will be simplified solutions.
The following is a personal summary:
When a technology is relatively complex, someone will come out to define a set of standards to simplify the technology and improve production efficiency.
No matter how carefully the designer considers it, once the standard is defined, flexibility will be lost.
Standards may conflict with your personal habits. You must learn to accept standards and return to the WPF topic. Then the solution in WPF is dependency property (DependencyProperty)
Okay, this article is just an introduction. If you have encountered these problems when using attributes, you will have some feelings.
If you realize that the above problems really exist with attributes, then this article has completed its task.
This article is not intended to be expanded upon. The next article will introduce the WPF dependency property system again.
Everyone is welcome to discuss.