控制模板中的文本自动调整大小

发布于 2024-09-06 17:23:02 字数 2521 浏览 2 评论 0原文

我正在为按钮创建一个模板。我希望放置在按钮内的文本随着按钮的大小而增大和缩小。

我的风格:

<Style x:Key="BigRoundButtonWithNumber" TargetType="{x:Type Button}">
    <Setter Property="OverridesDefaultStyle" Value="False"/>
    <Setter Property="Foreground" Value="White" />
    <Setter Property="FontWeight" Value="Bold" />
    <Setter Property="FontFamily" Value="Arial" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Grid Height="auto" Width="auto">
                    <Ellipse >
                        <Ellipse.Fill>
                            <SolidColorBrush Color="Gray"></SolidColorBrush>
                        </Ellipse.Fill>
                    </Ellipse>
                    <Ellipse  Margin ="10,10,10,10">
                        <Ellipse.Fill>
                            <RadialGradientBrush GradientOrigin="0.496,1.052">
                                <RadialGradientBrush.RelativeTransform>
                                    <TransformGroup>
                                        <ScaleTransform CenterX="0.5" CenterY="0.5" ScaleX="1.5" ScaleY="1.5"/>
                                        <TranslateTransform X="0.02" Y="0.3"/>
                                    </TransformGroup>
                                </RadialGradientBrush.RelativeTransform>
                                <GradientStop Offset="0.2" Color="White"/>
                                <GradientStop Offset="1" Color="Green"/>
                            </RadialGradientBrush>
                        </Ellipse.Fill>
                    </Ellipse>
                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Content="{TemplateBinding Content}" />
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

我的按钮:

<Button Click="Button_Click" Style="{DynamicResource BigRoundButtonWithNumber}">
                50
            </Button>

屏幕截图 http://img149.imageshack.us/img149/152 /unbenanntab.png

谢谢!

飞鸟

I am creating a template for a button. I want the text that is placed inside the button to grow and shrink with the size of the button.

My style:

<Style x:Key="BigRoundButtonWithNumber" TargetType="{x:Type Button}">
    <Setter Property="OverridesDefaultStyle" Value="False"/>
    <Setter Property="Foreground" Value="White" />
    <Setter Property="FontWeight" Value="Bold" />
    <Setter Property="FontFamily" Value="Arial" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Grid Height="auto" Width="auto">
                    <Ellipse >
                        <Ellipse.Fill>
                            <SolidColorBrush Color="Gray"></SolidColorBrush>
                        </Ellipse.Fill>
                    </Ellipse>
                    <Ellipse  Margin ="10,10,10,10">
                        <Ellipse.Fill>
                            <RadialGradientBrush GradientOrigin="0.496,1.052">
                                <RadialGradientBrush.RelativeTransform>
                                    <TransformGroup>
                                        <ScaleTransform CenterX="0.5" CenterY="0.5" ScaleX="1.5" ScaleY="1.5"/>
                                        <TranslateTransform X="0.02" Y="0.3"/>
                                    </TransformGroup>
                                </RadialGradientBrush.RelativeTransform>
                                <GradientStop Offset="0.2" Color="White"/>
                                <GradientStop Offset="1" Color="Green"/>
                            </RadialGradientBrush>
                        </Ellipse.Fill>
                    </Ellipse>
                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Content="{TemplateBinding Content}" />
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

my Button:

<Button Click="Button_Click" Style="{DynamicResource BigRoundButtonWithNumber}">
                50
            </Button>

Screenshot http://img149.imageshack.us/img149/152/unbenanntab.png

Thanks!

Tobi

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

那伤。 2024-09-13 17:23:02

如果您使用 ViewBox,它将调整大小以填充空间 - 将您的 ContentPresenter 更改为以下内容。

<Viewbox Panel.ZIndex="99">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Content="{TemplateBinding Content}" />
</Viewbox>

If you use a ViewBox it will resize to fill the space - change your ContentPresenter to the following.

<Viewbox Panel.ZIndex="99">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Content="{TemplateBinding Content}" />
</Viewbox>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文