我在我的应用程序中使用了一个自定义按钮控件,并且每个人的文本换行都设置为换行以用于翻译目的。测试时,我发现一些不同语言的字符串按照我的规范换行,尽管按钮高度保持不变,因此第二行文本被截断了。现在我从 ResourceDictionary 中的自定义按钮样式设置 xaml 中的每个按钮样式,但我无法弄清楚如何从 ResourceDictionary 相应地调整按钮高度(在这种情况下,我只需更改一次按钮样式,而不是手动更改在我能做的每一个按钮上)。我将如何通过资源字典中的自定义按钮样式进行更新,以便高度根据文本的换行时间自动调整?按钮实现示例如下:
主页.xaml
<Button x:Name="btnUseEmailAddress" Style="{StaticResource GlassButton}" Click="btnUseEmailAddress_Click" Margin="0,10">
<Button.Content>
<TextBlock TextWrapping="Wrap" Text="{Binding Path=LocalizedResources.MainPage_Contacts_UseContactsEmailAddress, Source={StaticResource LocalizedStrings}}"/>
</Button.Content>
</Button>
资源字典.xaml
<Style x:Key="GlassButton" TargetType="Button">
<Setter Property="FontSize" Value="25.33" />
<Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}" />
<Setter Property="Height" Value="60"/>
<Setter Property="Padding" Value="10,3,10,5"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border x:Name="ButtonBorder"
CornerRadius="30"
BorderThickness="4,4,4,4"
Background="#AA000000"
BorderBrush="#99FFFFFF"
RenderTransformOrigin="0.5,0.5">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="1.7*"/>
</Grid.RowDefinitions>
<Border Grid.Row="0" CornerRadius="23,23,0,0">
<Border.Background>
<LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
<GradientStop Color="#08FFFFFF" Offset="0"/>
<GradientStop Color="#88FFFFFF" Offset="1"/>
</LinearGradientBrush>
</Border.Background>
</Border>
<ContentPresenter x:Name="ButtonContentPresenter"
VerticalAlignment="Center"
Grid.RowSpan="2"
HorizontalAlignment="Center"/>
</Grid>
</Border>
<!--<ControlTemplate.Triggers>
<Trigger Property="IsPressed" Value="True">
<Setter Property="RenderTransform" TargetName="ButtonBorder">
<Setter.Value>
<TransformGroup>
<ScaleTransform ScaleX="0.9" ScaleY="0.9"/>
</TransformGroup>
</Setter.Value>
</Setter>
</Trigger>
</ControlTemplate.Triggers>-->
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
需要注意的是,Glass 按钮样式目前看起来很棒并且正是我想要的,除了文本被换行时。我可以在 MainPage.xaml 中使用将按钮高度调整为自动,<Button x:Name="btnUseEmailAddress" Height="Auto"...
但我必须为每个按钮执行此操作(这很多!)。那么有什么方法可以在玻璃按钮样式中实现这一点?