【问题标题】:how to change the font size of all text box within a grid, windows app uwp如何更改网格内所有文本框的字体大小,windows app uwp
【发布时间】:2017-01-17 11:22:49
【问题描述】:

我知道如何使用 HTML 和 CSS 开发网页,但我是 windows 应用程序开发的新手,我正在尝试开发通用 windows 平台 (UWP) 应用程序。

假设我有网格,

 <Grid Name="Grid1">
    <VisualStateManager.VisualStateGroups>
            <VisualStateGroup>
                <VisualState x:Name="Normal">
                    <VisualState.StateTriggers>
                        <AdaptiveTrigger MinWindowWidth="600" />
                </VisualState.StateTriggers> 
                <VisualState.Setters>
                    <Setter Target="text1.FontSize" Value="12" />
                    <Setter Target="text2.FontSize" Value="12" />
                    <Setter Target="text3.FontSize" Value="10" />                        
                </VisualState.Setters>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
     <Grid.RowDefinitions>
        <RowDefinition></RowDefinition>
     </Grid.RowDefinitions>
    <TextBlock Name="text1" FontSize="22" Text="hello text">    </TextBlock>
    <TextBlock Name="text2" FontSize="22" Text="hello text1">    </TextBlock>
    <TextBlock Name="text3" FontSize="22" Text="hello text2">    </TextBlock>
</Grid>

是否可以使用单个 VisualState.Setters 更改所有文本框的字体大小,就像我们在 HTML 中使用 CSS 类所做的那样?因为我必须根据窗口宽度更改许多文本框的字体大小。

对不起,如果这个问题太愚蠢和荒谬。提前致谢。

【问题讨论】:

    标签: c# .net windows uwp uwp-xaml


    【解决方案1】:

    您可以使用样式为所有控件应用相同的值。考虑下一个例子:

    <Grid>
        <Grid.Resources>
            <Style TargetType="TextBlock">
                <Setter Property="FontSize" Value="22"/>
            </Style>
        </Grid.Resources>
    
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
    
        <TextBlock Grid.Row="0" Text="hello text" />
        <TextBlock Grid.Row="1" Text="hello text1" />
        <TextBlock Grid.Row="2" Text="hello text2" />
    </Grid>
    

    已编辑

    您可以在 VisualState 的 Setter 中设置命名样式:

    <Grid Name="Grid1">
        <Grid.Resources>
            <Style x:Key="TextBlockStyle" TargetType="TextBlock">
                <Setter Property="FontSize" Value="22"/>
            </Style>
        </Grid.Resources>
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup>
                <VisualState x:Name="Normal">
                    ...
                    <VisualState.Setters>
                        <Setter Target="text1.Style" Value="{StaticResource TextBlockStyle}" />
                        ...
                    </VisualState.Setters>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
        ...
    </Grid>
    

    【讨论】:

    • 但我想在 VisualStateManager 中实现这一点,这样我就可以拥有响应式 UI。这在 VisualStateManager 中不起作用。
    • 您可以将所有控件放在某个面板中,并在“Setter”中更改此面板的属性(可能是附加属性)。如果没有任何子控件,则所有子控件都将继承此值。在您的示例中,您可以使用 TextElement.FontSize:
    猜你喜欢
    • 2011-12-08
    • 1970-01-01
    • 2019-02-22
    • 2021-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多