Silverlight DatePickerのテキストからDateTimeへの変換のインターセプト
-
05-07-2019 - |
質問
DatePicker
で次のテキストを DateTime
(国際形式で表示)に変換して、顧客が DatePicker
で日付を書き込めるようにしますテキストボックスが高速なので、 DateTime.Parse
だけではありません。
"3" to 2009-10-03
"14" to 2009-10-14
"1403" to 2009-03-14
"140310" to 2010-03-14
"14032010" to 2010-03-14
別の方法を試してみましたが、機能しません。 DatePicker.Text
/ DatePicker.SelectedDate
/ DatePicker.DisplayDate
をカスタム値コンバーターにバインドしようとしました。しかし、テキストに到達する前に DatePicker
がすでにテキストを処理しているため、機能しません。
DatePicker
TextBox.LostFocus
で次のように変換しようとしました:
public class CustomDatePicker : DatePicker
{
private DatePickerTextBox textBox;
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
textBox = this.GetTemplateChild("TextBox") as DatePickerTextBox;
if (textBox != null)
textBox.LostFocus += textBox_LostFocus;
}
void textBox_LostFocus(object sender, RoutedEventArgs e)
{
if (textBox == null) return;
DateTime result;
// This is my method for converting my special cases,
// parses d, dd, mm, ddmm, ddmmyy, ddmmyyyy
if (textBox.Text.TryParseShortcut(out result))
{
// I have also tried with
// this.SelectedDate/DisplayDate = result;
textBox.Text = result.ToString("dd/MM/yyyy");
return;
}
if (DateTime.TryParse(textBox.Text, out result))
{
// I have also tried with
// this.SelectedDate/DisplayDate = result;
textBox.Text = result.ToString("dd/MM/yyyy");
return;
}
}
}
アイデアはありますか
解決
Bindingを使用してデータをDatePickerにバインドする場合、ValueConverterを使用できます。
日付コンバーターの簡単な例:
public class CustomDateValueConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is string)
{
string dateValue = (string)value;
switch (dateValue.Length)
{
case 1:
case 2:
return new DateTime(DateTime.Now.Year, DateTime.Now.Month, System.Convert.ToInt32(dateValue)).ToString("dd/MM/yyyy");
//...
}
}
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
// here you should implement your logic to convert your value back.
return value;
}
}
XAMLのDatePickerTextBoxコントロール:
<controls:DatePickerTextBox Text="{Binding Path=Data, Mode=TwoWay, Converter={StaticResource CustomDateValueConverter}}" />
csのDatePickerTextBox:
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
DataContext = new MyData() { Data = "3" };
_textBox.LostFocus += (se, ea) =>
{
_textBox.DataContext =
new MyData() { Data = _textBox.Text };
};
}
public class MyData
{
public string Data { get; set; }
}
このサンプルでデータとして使用するMyDataというクラスを作成しました。
他のヒント
フルコントロールのソースを実際に起動してカスタマイズする必要があるかもしれません。問題は、TextChangedイベントでテキストが更新されることです:
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
if (this._textBox != null)
{
this.SetValueNoCallback(TextProperty, this._textBox.Text);
}
}
したがって、失われたフォーカスコードに到達するまでに、値は既に設定されています。サブクラス化を使用してこれを回避できましたが、TextChangedがすべての変更を処理するため、解析メソッドが機能するかどうかわかりません(1つの数字を押すとすぐに日付に変更されます)。
public class CustomDatePicker : DatePicker
{
private TextBox textBox;
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
textBox = this.GetTemplateChild("TextBox") as TextBox;
if (textBox != null)
textBox.TextChanged += textBox_TextChanged;
}
void textBox_TextChanged(object sender, TextChangedEventArgs e)
{
if (textBox.Text == "now")
{
// I have also tried with
// this.SelectedDate/DisplayDate = result;
textBox.Text = DateTime.Now.ToShortDateString();
}
}
}
単語全体を綴るまで何も変わらないので、私にとっては機能しますが、あなたの場合はおそらく機能しません。