private void SetWatermark(DatePicker dp) { if (dp == null) { return; } // force visual tree to be built, even if control is not visible dp.ApplyTemplate(); var tb = dp.GetChildOfType <DatePickerTextBox>(); if (tb == null) { return; } // force visual tree to be built, even if control is not visible tb.ApplyTemplate(); var wm = tb.Template.FindName("PART_Watermark", tb) as ContentControl; if (wm == null) { return; } wm.Content = Localization.Properties.Resources.Main_SelectADate; }
public void SelectedDate_null_Should_Use_Placeholders() { using (UnitTestApplication.Start(Services)) { DatePicker datePicker = new DatePicker { Template = CreateTemplate(), YearVisible = false }; datePicker.ApplyTemplate(); Threading.Dispatcher.UIThread.RunJobs(); var desc = datePicker.GetVisualDescendants(); Assert.True(desc.Count() > 1);//Should be layoutroot grid & button TextBlock yearText = null; TextBlock monthText = null; TextBlock dayText = null; Grid container = null; Assert.True(desc.ElementAt(1) is Button); container = (desc.ElementAt(1) as Button).Content as Grid; Assert.True(container != null); for (int i = 0; i < container.Children.Count; i++) { if (container.Children[i] is TextBlock tb && tb.Name == "YearText") { yearText = tb; }
private DatePicker CreateControl() { var datePicker = new DatePicker { Template = CreateTemplate() }; datePicker.ApplyTemplate(); return(datePicker); }
private static void SetWatermarkInternal(DatePicker d, object value) { d.ApplyTemplate(); var textBox = d.Template.FindName("PART_TextBox", d) as Control; if (textBox != null) { textBox.ApplyTemplate(); var watermarkControl = textBox.Template.FindName("PART_Watermark", textBox) as ContentControl; if (watermarkControl != null) { watermarkControl.Content = value; } } }
private static void SetTextBoxBinding(DatePicker datePicker) { datePicker.ApplyTemplate(); var textBox = GetTemplateTextBox(datePicker); var customTextBox = GetCustomTextBox(textBox); /* When DatePicker gets focus it passes focus on to its DatePickerTextBox. Since it has been * replaced DatePicker is keeping focus. A workaround is to make it unfocusable. */ datePicker.Focusable = false; var binding = new Binding("SelectedDate") { RelativeSource = new RelativeSource { AncestorType = typeof(DatePicker) }, Converter = new DateTimeConverter(), ConverterParameter = new Tuple <DatePicker, string>(datePicker, GetDateFormat(datePicker)) }; customTextBox.SetBinding(TextBox.TextProperty, binding); customTextBox.KeyUp -= CustomTextBoxOnKeyUp; customTextBox.KeyUp += CustomTextBoxOnKeyUp; }
public void MonthVisible_False_Should_Hide_Month() { using (UnitTestApplication.Start(Services)) { DatePicker datePicker = new DatePicker { Template = CreateTemplate(), MonthVisible = false }; datePicker.ApplyTemplate(); Threading.Dispatcher.UIThread.RunJobs(); var desc = datePicker.GetVisualDescendants(); Assert.True(desc.Count() > 1);//Should be layoutroot grid & button TextBlock monthText = null; Grid container = null; Assert.True(desc.ElementAt(1) is Button); container = (desc.ElementAt(1) as Button).Content as Grid; Assert.True(container != null); for (int i = 0; i < container.Children.Count; i++) { if (container.Children[i] is TextBlock tb && tb.Name == "MonthText") { monthText = tb; break; } } Assert.True(monthText != null); Assert.True(!monthText.IsVisible); Assert.True(container.ColumnDefinitions.Count == 3); } }