首页 > 解决方案 > 在 xamarin 表单中更改 Windows.UI.Xaml.Controls.DatePicker 弹出窗口的 F​​ontSize

问题描述

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

在此处输入图像描述

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

在此处输入图像描述

这是渲染器代码:

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 弹出窗口的字体大小的任何想法?

标签: xamarinxamarin.formsuwpdatepicker

解决方案


要更改弹出视图的字体大小,您可以参考这个案例。弹出视图的控制是LoopingSelector。默认字体大小为 15,LoopingSelector样式如下。

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

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

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

推荐阅读