问题
I know how to develop webpages with HTML and CSS, but I am new to the windows app development and I am trying to develop an universal windows platform (UWP) application.
Lets say I have grid,
<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>
Is it is possible to change the font size of all text box with single VisualState.Setters like how we do in with CSS class in HTML ?, because I have to change many text box font size based on window width.
Sorry if if the question is too silly and absurd. Thanks in advance.
回答1:
You can use styles to apply same values for all controls. Consider next example:
<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>
Edited
You can set a named style in a Setter of VisualState:
<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>
来源:https://stackoverflow.com/questions/39409365/how-to-change-the-font-size-of-all-text-box-within-a-grid-windows-app-uwp