Skip to content

Commit

Permalink
Changing TimePicker text box to a derived class (#1579)
Browse files Browse the repository at this point in the history
This matches how the DatePicker works and prevents implicit styles on TextBox from accidentally affecting it.
  • Loading branch information
Keboo authored Dec 31, 2019
1 parent 8f98497 commit 67d0b37
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
<converters:MathConverter Operation="Divide" x:Key="DivisionMathConverter" />

<Style x:Key="MaterialDesignTimePicker" TargetType="{x:Type wpf:TimePicker}">
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type FrameworkElement}}, Path=(TextElement.Foreground)}"/>
<Setter Property="BorderBrush" Value="{DynamicResource MaterialDesignTextBoxBorder}" />
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderThickness" Value="0 0 0 1"/>
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type FrameworkElement}}, Path=(TextElement.Foreground)}"/>
<Setter Property="BorderBrush" Value="{DynamicResource MaterialDesignTextBoxBorder}" />
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderThickness" Value="0 0 0 1"/>
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="IsTabStop" Value="False"/>
<Setter Property="wpf:HintAssist.Hint" Value="Select time" />
<Setter Property="ClockStyle" Value="{DynamicResource MaterialDesignClock}" />
Expand Down Expand Up @@ -100,7 +100,7 @@
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBox BorderThickness="0" x:Name="PART_TextBox"
<wpf:TimePickerTextBox BorderThickness="0" x:Name="PART_TextBox"
wpf:TextFieldAssist.TextBoxViewMargin=".5 0 0 0"
Margin="0"
Template="{StaticResource TextBoxTemplate}"
Expand Down
21 changes: 10 additions & 11 deletions MaterialDesignThemes.Wpf/TimePicker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace MaterialDesignThemes.Wpf
{
[TemplatePart(Name = ButtonPartName, Type = typeof(Button))]
[TemplatePart(Name = PopupPartName, Type = typeof(Popup))]
[TemplatePart(Name = TextBoxPartName, Type = typeof(TextBox))]
[TemplatePart(Name = TextBoxPartName, Type = typeof(TimePickerTextBox))]
public class TimePicker : Control
{
public const string ButtonPartName = "PART_Button";
Expand All @@ -36,10 +36,12 @@ static TimePicker()

public TimePicker()
{
_clock = new Clock {
_clock = new Clock
{
DisplayAutomation = ClockDisplayAutomation.ToMinutesOnly
};
_clockHostContentControl = new ContentControl {
_clockHostContentControl = new ContentControl
{
Content = _clock
};
InitializeClock();
Expand Down Expand Up @@ -172,7 +174,7 @@ private static void OnIsDropDownOpenChanged(DependencyObject d, DependencyProper
//TODO set time
//dp._originalSelectedDate = dp.SelectedDate;

timePicker.Dispatcher.BeginInvoke(DispatcherPriority.Input, new Action(() =>
timePicker.Dispatcher?.BeginInvoke(DispatcherPriority.Input, new Action(() =>
{
timePicker._clock.Focus();
}));
Expand Down Expand Up @@ -438,14 +440,13 @@ private string DateTimeToString(DateTime datetime, DatePickerFormat format)

private void PopupOnPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs mouseButtonEventArgs)
{
var popup = sender as Popup;
if (popup == null || popup.StaysOpen) return;
if (!(sender is Popup popup) || popup.StaysOpen) return;

if (_dropDownButton?.InputHitTest(mouseButtonEventArgs.GetPosition(_dropDownButton)) != null)
{
// This popup is being closed by a mouse press on the drop down button
// The following mouse release will cause the closed popup to immediately reopen.
// Raise a flag to block reopeneing the popup
// Raise a flag to block re-opening the popup
_disablePopupReopen = true;
}
}
Expand Down Expand Up @@ -495,10 +496,8 @@ private void InitializeClock()

private void ClockChoiceMadeHandler(object sender, ClockChoiceMadeEventArgs clockChoiceMadeEventArgs)
{
if (
(WithSeconds && (clockChoiceMadeEventArgs.Mode == ClockDisplayMode.Seconds)) ||
(!WithSeconds && (clockChoiceMadeEventArgs.Mode == ClockDisplayMode.Minutes))
)
if (WithSeconds && clockChoiceMadeEventArgs.Mode == ClockDisplayMode.Seconds ||
!WithSeconds && clockChoiceMadeEventArgs.Mode == ClockDisplayMode.Minutes)
{
TogglePopup();
if (SelectedTime == null)
Expand Down
13 changes: 13 additions & 0 deletions MaterialDesignThemes.Wpf/TimePickerTextBox.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Windows;
using System.Windows.Controls;

namespace MaterialDesignThemes.Wpf
{
public class TimePickerTextBox : TextBox
{
static TimePickerTextBox()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(TimePickerTextBox), new FrameworkPropertyMetadata(typeof(TimePickerTextBox)));
}
}
}

0 comments on commit 67d0b37

Please sign in to comment.