居然真的看到有人在用这个神器了。你们是从4.0之前版本迁移过来的项目吗?
关于你提到的问题。示例解决代码如下:- public partial class Form1 : Form
- {
- private Date _date1;
- public Form1()
- {
- InitializeComponent();
- //
- // Normally init some Interop.Date.
- //
- Date date1 = new Date();
- this.Controls.Add(date1);
- this._date1 = date1;
- _date1.Width += 17;
- _date1.Spin.Visible = Visibility.ShowAlways;
- _date1.Format.Pattern = "ggg ee年MM月dd日";
- //
- // Magic starts from here:
- //
- var gcDateTime = _date1.InnerEditor;
- for (var i = 0; i < gcDateTime.SideButtons.Count; i++)
- {
- var sideButton = gcDateTime.SideButtons[i] as GrapeCity.Win.Editors.SpinButton;
- if (sideButton != null)
- {
- sideButton.IsDefaultBehavior = false;
- sideButton.SpinDown += delegate
- {
- MoveCaretToDateTimeMonthField(gcDateTime);
- gcDateTime.ShortcutSpinDown();
- };
- sideButton.SpinUp += delegate
- {
- MoveCaretToDateTimeMonthField(gcDateTime);
- gcDateTime.ShortcutSpinUp();
- };
- }
- }
- }
- private static void MoveCaretToDateTimeMonthField(GrapeCity.Win.Editors.GcDateTime gcDateTime)
- {
- var monthField = gcDateTime.Fields.Find<GrapeCity.Win.Editors.Fields.DateMonthField>();
- if (monthField != null)
- {
- var selectionStart = 0;
- foreach (GrapeCity.Win.Editors.Fields.DateField field in gcDateTime.Fields)
- {
- if (field == monthField)
- {
- break;
- }
- selectionStart += field.Text.Length;
- }
- gcDateTime.Select(selectionStart, 0);
- }
- }
- }
复制代码
主要思想是禁用默认的Spin行为,自己挂事件处理Spin行为。在处理函数中,随心所欲地将光标移动到合适的位置。
所使用到的主要黑技术有
1. InnerEditor对象其实封装了真正的GrapeCity.Win.Editors.GcDateTime实例
2. Spin按钮的行为其实是通过IsDefaultBehavior搞定的。
3. 怎么找计算光标的目标位置
希望你喜欢。 |