用法如下:
CustomDateFormat 只要是符合日期格式的都可以。如果乱写。就会显示有问题
<CustomControl:CustomDatePicker Margin="0,100,0,0" CustomDateFormat="MM/dd/yyyy"/>
代码如下
View Code
public class CustomDatePicker : DatePicker { public string CustomDateFormat { get { return (string)GetValue(CustomDateFormatProperty); } set { SetValue(CustomDateFormatProperty, value); } } // Using a DependencyProperty as the backing store for CustomDateFormat. This enables animation, styling, binding, etc... public static readonly DependencyProperty CustomDateFormatProperty = DependencyProperty.Register("CustomDateFormat", typeof(string), typeof(CustomDatePicker), new UIPropertyMetadata(string.Empty,new PropertyChangedCallback(OnCustomDateFormatChanged))); private static void OnCustomDateFormatChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { Thread.CurrentThread.CurrentCulture = (CultureInfo)Thread.CurrentThread.CurrentCulture.Clone(); Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern = e.NewValue.ToString(); Thread.CurrentThread.CurrentCulture.DateTimeFormat.LongDatePattern = e.NewValue.ToString(); } }