如何在TimePicker中更改文本和下划线之间的距离?

时间:2017-03-29 19:27:10

标签: wpf visual-studio xaml timepicker

目前文本和下划线之间有12个像素用于timepicker,我想将其设置为7像素。

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

在资源词典中,我对样式进行了以下更改:

<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没有工作

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

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

似乎保证金是问题,但是当我可以覆盖其他所有内容时,我无法覆盖它

3 个答案:

答案 0 :(得分:1)

您必须覆盖整个控制模板:

<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>

在模板内部,您应该使用名称x来隐藏Button:名称=“PART_Button”(对于exmaple Height =“17”)或最接近按钮的文本框或x:Name =“border”padding属性。

答案 1 :(得分:1)

在app.xaml中添加高度并验证它。它对我有用。您可以增加或减少高度以将文本设置在所需位置。

      <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" />
            <Setter Property="Height" Value="22" />
        </Style>

答案 2 :(得分:0)

完成答案=

<Style x:Key="TimePicker" TargetType="{x:Type materialDesign:TimePicker}">     
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type materialDesign:TimePicker}">
                <ControlTemplate.Resources>
                    <ControlTemplate x:Key="TextBoxTemplate" TargetType="{x:Type TextBox}">
                        <Grid>
                            <ScrollViewer x:Name="PART_ContentHost" Focusable="false" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden" />
                            <materialDesign:SmartHint x:Name="Hint"
                                           FontSize="{TemplateBinding FontSize}"
                                           UseFloating="{Binding Path=(materialDesign:HintAssist.IsFloating), RelativeSource={RelativeSource TemplatedParent}}"
                                           Hint="{Binding Path=(materialDesign:HintAssist.Hint), RelativeSource={RelativeSource TemplatedParent}}"
                                           HintProxy="{Binding RelativeSource={RelativeSource TemplatedParent}, Converter={x:Static converters:HintProxyFabricConverter.Instance}}"
                                           HintOpacity="{Binding Path=(materialDesign: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}" 
                            Padding="0"
                            SnapsToDevicePixels="True">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="Auto" />
                            </Grid.ColumnDefinitions>
                            <TextBox BorderThickness="0" x:Name="PART_TextBox"                                                                               
                                     materialDesign: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 materialDesign:CustomPopupPlacementCallbackHelper.LargePopupCallback}"
                                   PlacementTarget="{Binding ElementName=PART_TextBox}" StaysOpen="False"
                                   PopupAnimation="Fade"/>
                        </Grid>
                    </Border>
                    <materialDesign:Underline x:Name="Underline" Visibility="{Binding Path=(materialDesign: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>