WPF interface development skills sharing-how to implement a custom DateEdit and automatically correct the value

Download DevExpress v20.1 full version DevExpress v20.1 Chinese resource acquisition 

With DevExpress WPF Controls , you can create XAML-based applications with powerful interactive functions that focus on the needs of contemporary customers and build a new generation of touch-enabled solutions in the future.

Problems encountered

When using the DateEdit control, because the control masks "autocorrects" the value entered by the user, if the user enters 2020-04-31, he will get 2020-04-01. In this case, the user may enter an incorrect date, and without any warning to notify the user, he will continue to execute the incorrect date. It is best to let the user enter an invalid date and display a validation error on the editor. The editor should maintain its format, and the cursor should be the same as the original DateEdit control, jumping from one part of the date to another. The only difference is that you can enter invalid dates corresponding to the format and get validation errors. How can this be achieved?

solution:

DateEdit is a text editor with a DateTime type mask that does not allow you to enter the wrong date, which will cause the behavior described above.

In order to achieve the goal, you can create PopupBaseEdit with a mask:

<dxe:PopupBaseEdit Mask="\d{4}-\d{2}-\d{2}" 
MaskType="RegEx" 
Validate="DateEdit_Validate"...>
<dxe:PopupBaseEdit.PopupContentTemplate>
...
</dxe:PopupBaseEdit.PopupContentTemplate>
</dxe:PopupBaseEdit>

 

To make PopupBaseEdit look like DateEdit, it is recommended to use the PopupContentTemplate property.

The DateEditCalendar used in the PopupBaseEdit pop-up window uses the DateTime type, and the EditValue of PopupBaseEdit contains a String, so when binding this value, you need to create a custom Converter. Please note that the appropriate date format needs to be passed to the converter. For example, you can perform this operation through ConverterParameter.


DevExpress Technical Exchange Group 2: 775869749 Welcome to join the group discussion

Huidu high-end UI interface development

Guess you like

Origin blog.csdn.net/AABBbaby/article/details/107950082