使用自定义模板从按钮中删除文本

时间:2017-10-09 14:43:11

标签: c# .net wpf

我想创建一个按钮样式,用户将鼠标悬停在鼠标上,它将其背景更改为我设置的图像,但是当我使用此样式时,它也会删除文本(内容)

以下是我使用的样式

<Style TargetType="{x:Type Button}" x:Key="StartButton_Style">

        <Setter Property="Background">

            <!-- Background image (dark one)-->
            <Setter.Value>

                <ImageBrush ImageSource="C:\Users\yosi1\OneDrive\מסמכים\Visual Studio 2017\Projects\Classic Story Launcher\Resources\Start_Button_Nornal.png"/>

            </Setter.Value>
        </Setter>

        <Setter Property="FontFamily" Value="Candara"/>

        <Setter Property="FontSize"   Value="40"/>

        <Setter Property="Foreground" Value="#FFA48B60"/>

        <Setter Property="Template">

            <Setter.Value>

                <ControlTemplate TargetType="{x:Type Button}">

                    <Border Background="{TemplateBinding Background}"/>


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

        <!-- Triggers an event when the user hovers the mouse over the button-->
        <Style.Triggers>

            <Trigger Property="IsMouseOver" Value="True">

                <!-- Chagnes the background images to notify the user that his mouse is over the button -->
                <Setter Property="Background">

                    <Setter.Value>

                        <ImageBrush ImageSource="C:\Users\yosi1\OneDrive\מסמכים\Visual Studio 2017\Projects\Classic Story Launcher\Resources\Start_Button_Click.png"/>

                    </Setter.Value>

                </Setter>

            </Trigger>

        </Style.Triggers>

    </Style>

以下是使用样式的按钮元素

<Button Grid.Column="2" Content="START" Margin="0,34,0,0" Style="{StaticResource StartButton_Style}"/>

1 个答案:

答案 0 :(得分:0)

在模板中添加ContentPresenter

<Style TargetType="{x:Type Button}" x:Key="StartButton_Style">
    <Setter Property="Background">
        <Setter.Value>
            <ImageBrush ImageSource="C:\Users\yosi1\OneDrive\מסמכים\Visual Studio 2017\Projects\Classic Story Launcher\Resources\Start_Button_Nornal.png"/>
        </Setter.Value>
    </Setter>
    <Setter Property="FontFamily" Value="Candara"/>
    <Setter Property="FontSize"   Value="40"/>
    <Setter Property="Foreground" Value="#FFA48B60"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border Background="{TemplateBinding Background}">
                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background">
                <Setter.Value>
                    <ImageBrush ImageSource="C:\Users\yosi1\OneDrive\מסמכים\Visual Studio 2017\Projects\Classic Story Launcher\Resources\Start_Button_Click.png"/>
                </Setter.Value>
            </Setter>
        </Trigger>
    </Style.Triggers>
</Style>