无法在WPF中使用样式在按钮上设置文本 [英] Can not set text on a button using style in WPF

查看:78
本文介绍了无法在WPF中使用样式在按钮上设置文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的按钮控件



this is my button control

<Button Grid.Row="1"  Height="49" Width="249" Style="{StaticResource TriggerStyleBlue}" Name="btnStart" Click="btnStart_Click" Cursor="Hand" VerticalContentAlignment="Top" Content="Start Now" Foreground="White" VerticalAlignment="Top" HorizontalAlignment="Left" />





i无法在按钮上设置文字



我的尝试:



我的风格





i can not set text on the button

What I have tried:

my style

<Style x:Key="TriggerStyleBlue" TargetType="{x:Type Button}">
            <Setter Property="Foreground">
                <Setter.Value>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                        <GradientStop Color="#9B000000" Offset="0"/>
                        <GradientStop Color="White" Offset="1"/>
                    </LinearGradientBrush>
                </Setter.Value>
            </Setter>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border TextBlock.Foreground="{TemplateBinding Foreground}"
                                  x:Name="Border"
                                  CornerRadius="1"
                                  BorderThickness="1">
                            <Border.Background>
                                <ImageBrush ImageSource= "..\Resources\BTN_REGistryscan_Normal.jpg"  />
                            </Border.Background>
                            <!--<Border.Effect>
                                <DropShadowEffect/>
                            </Border.Effect>-->
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsPressed" Value="True">
                                <!--<Setter TargetName="Border" Property="Border.Effect" Value="{x:Null}"/>-->
                                <Setter  Property="Background" TargetName="Border">
                                    <Setter.Value>
                                        <ImageBrush ImageSource="..\Resources\BTN_REGistryscan_Down.jpg"/>
                                    </Setter.Value>
                                </Setter>
                            </Trigger>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="Background" TargetName="Border">
                                    <Setter.Value>
                                        <ImageBrush ImageSource="..\Resources\BTN_REGistryscan_Hover.jpg"/>
                                    </Setter.Value>
                                </Setter>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>





我做错了什么?



What iam doing wrong ?

推荐答案

添加以下xaml解决问题

Add following xaml solve problem
</Border.Background>
                            <ContentPresenter x:Name="cp" 
                        HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                        Margin="{TemplateBinding Padding}"
                        VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                        RecognizesAccessKey="True"  />


这篇关于无法在WPF中使用样式在按钮上设置文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆