Search code examples
wpfvisual-studioxamltimepicker

How do I change the distance between text and underline in TimePicker?


There is currently 12 pixels between the text and underline for timepicker and I want to set it to 7 pixels.

<materialDesign:TimePicker  SelectedTime="{Binding LabCase.AptTime}" />

In resource dictionary I have made these changes to style:

<Style TargetType="materialDesign:TimePicker" BasedOn="{StaticResource {x:Type materialDesign:TimePicker}}">
     <Setter Property="HorizontalAlignment" Value="Left" />
     <Setter Property="MinWidth" Value="200" />
     <Setter Property="materialDesign:HintAssist.Hint" Value="" />
     <Setter Property="Margin" Value="0,-3,0,0" />
</Style>

Padding didn't work

<Setter Property="Padding" Value="0,7,0,0"/>

https://github.com/ButchersBoy/MaterialDesignInXamlToolkit/blob/master/MaterialDesignThemes.Wpf/Themes/MaterialDesignTheme.TimePicker.xaml

It seems like margin is the problem but I can't see to override it when I can override everything else


Solution

  • You will have to override this whole control template:

    <Style TargetType="materialDesign:TimePicker" BasedOn="{StaticResource {x:Type materialDesign:TimePicker}}">
         <Setter Property="HorizontalAlignment" Value="Left" />
         <Setter Property="MinWidth" Value="200" />
         <Setter Property="materialDesign:HintAssist.Hint" Value="" />
    <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type wpf:TimePicker}">
                        <ControlTemplate.Resources>
                            <ControlTemplate x:Key="TextBoxTemplate" TargetType="{x:Type TextBox}">
                                <Grid>
                                    <ScrollViewer x:Name="PART_ContentHost" Focusable="false" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"                                             
                                                  />
                                    <wpf:SmartHint x:Name="Hint"
                                                   FontSize="{TemplateBinding FontSize}"
                                                   UseFloating="{Binding Path=(wpf:HintAssist.IsFloating), RelativeSource={RelativeSource TemplatedParent}}"
                                                   Hint="{Binding Path=(wpf:HintAssist.Hint), RelativeSource={RelativeSource TemplatedParent}}"
                                                   HintProxy="{Binding RelativeSource={RelativeSource TemplatedParent}, Converter={x:Static converters:HintProxyFabricConverter.Instance}}"
                                                   HintOpacity="{Binding Path=(wpf:HintAssist.HintOpacity), RelativeSource={RelativeSource TemplatedParent}}"/>
                                </Grid>
                                <ControlTemplate.Triggers>
                                    <MultiTrigger>
                                        <MultiTrigger.Conditions>
                                            <Condition SourceName="Hint" Property="IsContentNullOrEmpty" Value="False" />
                                            <Condition Property="IsKeyboardFocused" Value="True" />
                                        </MultiTrigger.Conditions>
                                        <Setter TargetName="Hint" Property="Foreground" Value="{DynamicResource PrimaryHueMidBrush}" />
                                        <Setter TargetName="Hint" Property="HintOpacity" Value="1" />
                                    </MultiTrigger>
                                </ControlTemplate.Triggers>
                            </ControlTemplate>
                            <ControlTemplate x:Key="DropDownButtonTemplate" TargetType="{x:Type Button}">
                                <Grid Background="Transparent">
                                    <VisualStateManager.VisualStateGroups>
                                        <VisualStateGroup x:Name="CommonStates">
                                            <VisualStateGroup.Transitions>
                                                <VisualTransition GeneratedDuration="0"/>
                                                <VisualTransition GeneratedDuration="0:0:0.1" To="MouseOver"/>
                                                <VisualTransition GeneratedDuration="0:0:0.1" To="Pressed"/>
                                            </VisualStateGroup.Transitions>
                                            <VisualState x:Name="Normal"/>
                                            <VisualState x:Name="MouseOver"/>
                                            <VisualState x:Name="Pressed"/>
                                            <VisualState x:Name="Disabled"/>
                                        </VisualStateGroup>
                                    </VisualStateManager.VisualStateGroups>
                                    <Viewbox>
                                        <Canvas Width="24" Height="24">
                                            <Path Data="M12,20A8,8 0 0,0 20,12A8,8 0 0,0 12,4A8,8 0 0,0 4,12A8,8 0 0,0 12,20M12,2A10,10 0 0,1 22,12A10,10 0 0,1 12,22C6.47,22 2,17.5 2,12A10,10 0 0,1 12,2M12.5,7V12.25L17,14.92L16.25,16.15L11,13V7H12.5Z" Fill="{TemplateBinding Foreground}" />
                                        </Canvas>
                                    </Viewbox>
                                </Grid>
                            </ControlTemplate>
                        </ControlTemplate.Resources>
                        <Grid x:Name="TemplateRoot">
                            <Border BorderBrush="{TemplateBinding BorderBrush}"
                                    BorderThickness="{TemplateBinding BorderThickness}"
                                    Background="{TemplateBinding Background}" 
                                    x:Name="border"
                                    Padding="0 4 0 4"
                                    SnapsToDevicePixels="True">
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="*" />
                                        <ColumnDefinition Width="Auto" />
                                    </Grid.ColumnDefinitions>                           
                                    <TextBox BorderThickness="0" x:Name="PART_TextBox"                                                                               
                                             wpf:TextFieldAssist.TextBoxViewMargin=".5 0 0 0"
                                             Margin="0"                                     
                                             Template="{StaticResource TextBoxTemplate}"                                     
                                             />
                                    <Button Grid.Column="1" Foreground="{TemplateBinding BorderBrush}" Focusable="False" HorizontalAlignment="Right" Margin="4 0 0 -3" Grid.Row="0" Padding="0"
                                            x:Name="PART_Button"                            
                                            Height="17">
                                            Template="{StaticResource DropDownButtonTemplate}" />
                                    <Popup x:Name="PART_Popup" AllowsTransparency="True" 
                                           Placement="Custom"
                                           CustomPopupPlacementCallback="{x:Static wpf:CustomPopupPlacementCallbackHelper.LargePopupCallback}"
                                           PlacementTarget="{Binding ElementName=PART_TextBox}" StaysOpen="False"
                                           PopupAnimation="Fade"/>
                                </Grid>
                            </Border>
                            <wpf:Underline x:Name="Underline" Visibility="{Binding Path=(wpf:TextFieldAssist.DecorationVisibility), RelativeSource={RelativeSource TemplatedParent}}"/>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter Property="Opacity" TargetName="TemplateRoot" Value="0.56"/>
                            </Trigger>
                            <Trigger Property="IsMouseOver" Value="true">
                                <Setter Property="BorderBrush" Value="{DynamicResource PrimaryHueMidBrush}"/>
                            </Trigger>
                            <Trigger Property="IsKeyboardFocusWithin" Value="true">
                                <Setter Property="IsActive" TargetName="Underline" Value="True"/>
                                <Setter Property="BorderBrush"  Value="{DynamicResource PrimaryHueMidBrush}"/>
                            </Trigger>
                            <Trigger Property="Validation.HasError" Value="true">
                                <Setter Property="BorderBrush" Value="{DynamicResource ValidationErrorBrush}"/>
                                <Setter TargetName="Underline" Property="Background" Value="{DynamicResource ValidationErrorBrush}"/>
                            </Trigger>                        
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    

    Inside the template you should ovveride Button with name x:Name="PART_Button" ( for exmaple Height="17" ) or TextBox nearest to the button or x:Name="border"padding property.