更改Xamarin形式的Windows.UI.Xaml.Controls.DatePicker弹出窗口的FontSize

时间:2019-02-22 00:20:21

标签: xamarin xamarin.forms uwp datepicker

我在xamarin表单项目中有一个自定义渲染器,用于编辑UWP的DatePicker的样式。我已经解决了大小调整问题,现在我只是想在单击datepicker更新日期时尝试编辑datePicker弹出窗口中文本的字体大小。这就是我的意思:

enter image description here

我目前有以下自定义渲染器代码,用于更改基本datePicker条目控件的中间宽度和字体大小,使它看起来像

enter image description here

这是渲染器代码:

class MyDatePickerRenderer : DatePickerRenderer
{
    #region Parent override
    protected override void OnElementChanged(ElementChangedEventArgs<DatePicker> e)
    {
        base.OnElementChanged(e);
        if (e.OldElement != null || Element == null)
            return;

        if (Control != null)
        {
            Control.MinWidth = 150;                                
        }

        if (Element != null)
        {                
            Element.FontSize = 12;                     
        }
    }
    #endregion
}

关于如何更改datePicker弹出窗口的字体大小的任何想法吗?

1 个答案:

答案 0 :(得分:0)

要更改弹出视图的字体大小,可以参考此case。弹出视图的控件为LoopingSelector。并且默认字体大小为LoopingSelector样式的15,如下所示。

<Style TargetType="LoopingSelector">
    <Setter Property="ShouldLoop" Value="True" />
    <Setter Property="UseSystemFocusVisuals" Value="True" />
    <Setter Property="ItemTemplate">
        <Setter.Value>
            <DataTemplate>
                <StackPanel VerticalAlignment="Center">
                    <TextBlock Text="{Binding PrimaryText}" FontFamily="{ThemeResource ContentControlThemeFontFamily}" FontSize="15" />
                </StackPanel>
            </DataTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Control">
                <Grid>

                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Normal" />

                            <VisualState x:Name="PointerOver">

                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="UpButton" Storyboard.TargetProperty="Visibility">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="Visible" />
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="DownButton" Storyboard.TargetProperty="Visibility">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="Visible" />
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>

                    </VisualStateManager.VisualStateGroups>
                    <ScrollViewer x:Name="ScrollViewer"
                VerticalSnapPointsType="Mandatory"
                VerticalSnapPointsAlignment="Near"
                VerticalScrollBarVisibility="Hidden"
                HorizontalScrollMode="Disabled"
                ZoomMode="Disabled"
                Template="{StaticResource ScrollViewerScrollBarlessTemplate}" />
                    <RepeatButton x:Name="UpButton"
                Content="&#xE70E;"
                FontFamily="{ThemeResource SymbolThemeFontFamily}"
                FontSize="8"
                Height="22"
                Padding="0"
                HorizontalAlignment="Stretch"
                VerticalAlignment="Top"
                Visibility="Collapsed"
                Style="{StaticResource DateTimePickerFlyoutButtonStyle}"
                Background="{ThemeResource LoopingSelectorButtonBackground}"
                IsTabStop="False" />
                    <RepeatButton x:Name="DownButton"
                Content="&#xE70D;"
                FontFamily="{ThemeResource SymbolThemeFontFamily}"
                FontSize="8"
                Height="22"
                Padding="0"
                HorizontalAlignment="Stretch"
                VerticalAlignment="Bottom"
                Visibility="Collapsed"
                Style="{StaticResource DateTimePickerFlyoutButtonStyle}"
                Background="{ThemeResource LoopingSelectorButtonBackground}"
                IsTabStop="False" />

                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

如果要更改它。您只需将以下字体大小修改为另一个值。然后将完整样式放在xamarin uwp项目中的App.xaml文件的<Application.Resources>中。

<TextBlock Text="{Binding PrimaryText}" FontFamily="{ThemeResource ContentControlThemeFontFamily}" FontSize="20" />