【问题标题】:how to change the style of a textblock in a button如何更改按钮中文本块的样式
【发布时间】:2014-04-22 16:41:52
【问题描述】:

我需要更改按钮内的文本块的样式..

这是我的两种风格:

        <Style x:Key="btnStyleLR" TargetType="TextBlock">
            <Setter Property="FontFamily" Value="Segoe UI Light" />
            <Setter Property="FontSize" Value="40" />
            <Setter Property="Padding" Value="0,20,0,20" />
        </Style>

            <Style x:Key="btnStyleLROverride" BasedOn="{StaticResource btnStyleLR}" TargetType="TextBlock">
            <Setter Property="FontSize" Value="10" />
            <Setter Property="Padding" Value="0,10,0,10" />
        </Style>

然后:

        <Style x:Key="btnLoginRegister" TargetType="Button">
            <Setter Property="Width" Value="400" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Grid>
                            <Border x:Name="brd"
                                    BorderBrush="Orange"
                                    BorderThickness="1"
                                    CornerRadius="6">

                                <TextBlock HorizontalAlignment="Center"
                                           Style="{TemplateBinding Tag}"
                                           Text="{TemplateBinding Content}" />
                            </Border>
                        </Grid>

                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

我在按钮的标签属性中绑定了样式,但它不起作用。 然后我有两个按钮需要更改 de textblock 样式:

                    <Button x:Name="loginBtn"
                        Style="{StaticResource btnLoginRegister}"
                        Tag="btnStyleLROverride"
                        Content="Login" />

和其他:

            <Button x:Name="registerBtn"
                Content="Register"
                Tag="btnStyleLR"
                Style="{StaticResource btnLoginRegister}" />

还有其他方法可以通过有效的绑定更改样式吗?谢谢

【问题讨论】:

    标签: wpf silverlight xaml winrt-xaml


    【解决方案1】:

    你不应该像那样破坏按钮的模板,除非它只是为了这个问题的目的。解决您的问题的简单方法是简单地将 TextBlock 设置为按钮的内容,然后您可以更改其样式而无需修改模板。

    【讨论】:

    • Filip,模板有一些视觉状态会影响边框和文本块的样式,这是我能想到的唯一方法..也许是错误的..
    【解决方案2】:

    Tag 中,您必须提供资源价值,而不是字符串。应该是:

    <Button x:Name="loginBtn"
            Style="{StaticResource btnLoginRegister}"
            Tag="{StaticResource btnStyleLROverride}"
            Content="Login" />
    

    对其他按钮执行相同的操作。

    【讨论】:

      【解决方案3】:

      据我了解您的要求: 你有两个Buttons(“登录”和“注册”)需要在视觉上减少(不应该使用视觉状态)并且每个都有自己的“文本样式”(但可能共享一个基本样式)?它是否正确? 如果是这种情况,我推荐两个不同的Button Styles(可能共享相同的基本样式):

      <Button x:Name="loginBtn" Content="Login"
              Style="{StaticResource LoginButtonStyle}"/>
      <Button x:Name="registerBtn" Content="Register"
              Style="{StaticResource RegisterButtonStyle}"/>
      

      还有你的风格:

      <Style x:Key="PlainButtonStyle" TargetType="Button">
          <Setter Property="FontFamily" Value="Segoe UI Light"/>
          <Setter Property="Template">
              <Setter.Value>
                  <ControlTemplate TargetType="Button">
                      <Border BorderBrush="Orange" BorderThickness="1" CornerRadius="6">
                          <TextBlock Text="{TemplateBinding Content}"
                              Padding="{TemplateBinding Padding}"
                              HorizontalAlignment="Center"/>
                      </Border>
                  </ControlTemplate>
              </Setter.Value>
          </Setter>
      </Style>
      <Style x:Key="LoginButtonStyle"
             BasedOn="{StaticResource PlainButtonStyle}" TargetType="Button">
          <Setter Property="FontSize" Value="10"/>
          <Setter Property="Padding" Value="0,10,0,10"/>
      </Style>
      <Style x:Key="RegisterButtonStyle"
             BasedOn="{StaticResource PlainButtonStyle}" TargetType="Button">
          <Setter Property="FontSize" Value="40"/>
          <Setter Property="Padding" Value="0,20,0,20"/>
      </Style>
      

      FontSizeFontFamily 等属性被继承。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-02-10
        • 1970-01-01
        • 2014-09-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-16
        相关资源
        最近更新 更多