Category / Section
How to add a new button near Done and Cancel buttons in SfTimeSelector?
1 min read
<Grid <syncfusion:SfDatePicker HorizontalAlignment="Left" Name="datePicker" Loaded="datePicker_Loaded"/> <syncfusion:SfTimePicker HorizontalAlignment="Right" Name="TimePicker" Loaded="TimePicker_Loaded"/> </Grid>
A New button can be added, next to Done and Close buttons in the SfTimeSelector using
FindVisualChildrenofType().
public sealed partial class MainPage : Page { //private object dropdownpopup_Opened; public MainPage() { this.InitializeComponent(); } Popup dropdownPopup = null; Popup timepickerdropdownPopup = null; public static IEnumerable<T> FindVisualChildrenOfType<T>(DependencyObject parent) where T : DependencyObject { List<T> foundChildren = new List<T>(); int childCount = VisualTreeHelper.GetChildrenCount(parent); for (int i = 0; i < childCount; i++) { var child = VisualTreeHelper.GetChild(parent, i); T childType = child as T; if (childType == null) { foreach (var other in FindVisualChildrenOfType<T>(child)) yield return other; } else { yield return (T)child; } } } private void datePicker_Loaded(object sender, RoutedEventArgs e) { if (datePicker != null) { foreach (Popup popup in FindVisualChildrenOfType<Popup>(datePicker)) { if (popup != null && popup.Name == "PART_DropDown") { dropdownPopup = popup; dropdownPopup.Opened += dropdownpopup_Opened; } } } } private void TimePicker_Loaded(object sender, RoutedEventArgs e) { if (TimePicker != null) { foreach (Popup popup in FindVisualChildrenOfType<Popup>(TimePicker)) { if (popup != null && popup.Name == "PART_DropDown") { timepickerdropdownPopup = popup; timepickerdropdownPopup.Opened += dropdownpopup_Opened; } } } } private void dropdownpopup_Opened(object sender, object e) { if (dropdownPopup != null && dropdownPopup.Child != null && dropdownPopup.Child is Grid && (dropdownPopup.Child as Grid).Children.Count > 0) { SfDateSelector selector = null; SfTimeSelector timeselector = null; foreach (var child in (dropdownPopup.Child as Grid).Children) { if (child is SfDateSelector selector = child as SfDateSelector; if (selector != null) break; } foreach (var child in (timepickerdropdownPopup.Child as Grid).Children) { if (child is SfTimeSelector) timeselector = child as SfTimeSelector; if (timeselector != null) break; } if (selector != null) { selector.Style = Application.Current.Resources["dateselectorstyle"] as Style; } if (timeselector != null) timeselector.Style = Application.Current.Resources["timeselectorstyle"] as Style; } } }
Output