在样式中向按钮添加内容

时间:2016-12-09 14:57:54

标签: wpf button

您好我试图覆盖WPF上的默认鼠标悬停事件。

我已设法解决这个问题,但现在显示任何文本内容时出现问题

Style Setter:

<Style x:Key="MyButton" TargetType="Button">
        <Setter Property="OverridesDefaultStyle" Value="True"/>
        <Setter Property="Cursor" Value="Hand"/>
        <Setter Property="Content"  Value="Submit" />
        <Setter Property="Template">


            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border Name="Border" BorderThickness="0" BorderBrush="White" Background="#FF7AB800" Height="24" Margin="0,0,0.2,0" />



                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True" >

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

继承我的按钮代码:

 <Button x:Name="submitBtn" HorizontalAlignment="Left" Margin="402,296,0,0" VerticalAlignment="Top" Width="75" Foreground="Black" FontFamily="Calibri Light" FontSize="16" Style="{StaticResource MyButton}" Content="Submit"/>

2 个答案:

答案 0 :(得分:0)

首先,您需要添加x:type

  <Style x:Key="MyButton" TargetType="x:Type Button">
      <Setter Property="SnapsToDevicePixels" Value="True"/>
         <Border Name="Border" BorderThickness="0" BorderBrush="White" Background="#FF7AB800" Height="24" Margin="0,0,0.2,0" />
          <ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center"  />
         </Border>
               <ControlTemplate TargetType="x:Type Button">

答案 1 :(得分:0)

您错过了ContentPresenter。此外,我建议直接从样式中删除设置内容值。您希望在控件本身内执行此操作。

<Style x:Key="MyButton" TargetType="Button">
    <Setter Property="OverridesDefaultStyle" Value="True"/>
    <Setter Property="Cursor" Value="Hand"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border Name="Border" 
                        BorderThickness="0" 
                        BorderBrush="White" 
                        Background="#FF7AB800" 
                        Height="24" 
                        Margin="0,0,0.2,0">
                     <ContentPresenter Margin="2"
                            HorizontalAlignment="Center"
                            VerticalAlignment="Center"
                            RecognizesAccessKey="True" 
                            Content="{TemplateBinding Content}"/>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True" >

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

<Button x:Name="submitBtn" 
        HorizontalAlignment="Left" 
        Margin="402,296,0,0" 
        VerticalAlignment="Top" 
        Width="75" 
        Foreground="Black" 
        FontFamily="Calibri Light" 
        FontSize="16" 
        tyle="{StaticResource MyButton}" 
        Content="Submit"/>
相关问题