首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在XAML中使用ControlTemplate时按钮内容消失

在XAML中使用ControlTemplate时按钮内容消失
EN

Stack Overflow用户
提问于 2016-01-06 11:14:04
回答 1查看 1.3K关注 0票数 0

我为我的表单写了一种样式,一切看起来都很好,只是我的按钮上没有文字。我唯一能得到的就是一个没有内容的灰色按钮。

以下是我尝试过的:

代码语言:javascript
运行
复制
<Style x:Key="CodeButton" TargetType="{x:Type Button}" >
    <Setter Property="SnapsToDevicePixels" Value="True"/>
    <Setter Property="OverridesDefaultStyle" Value="True"/>
    <Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
    <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
    <Setter Property="Content" Value="Enter"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border 
                      Name="Border"
                      CornerRadius="4" 
                      Padding="10"
                      BorderBrush="#666666"
                      Background="#e5e5e5"
                      BorderThickness="1">
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsFocused" Value="True">
                        <Setter TargetName="Border" Property="BorderBrush" Value="#90c74b"/>
                        <Setter TargetName="Border" Property="BitmapEffect">
                            <Setter.Value>
                                <DropShadowBitmapEffect Color="#90c74b" Direction="0" ShadowDepth="4" Opacity="0.7" Softness="0.8" />
                            </Setter.Value>
                        </Setter>
                    </Trigger>

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

正如您在第6行看到的那样,我希望按钮的内容是"Enter“。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-01-06 11:18:45

为了显示文本,您需要在模板中添加一个ContentPresenter

试试这个:

代码语言:javascript
运行
复制
<Style x:Key="CodeButton" TargetType="{x:Type Button}" >
    <Setter Property="SnapsToDevicePixels" Value="True"/>
    <Setter Property="OverridesDefaultStyle" Value="True"/>
    <Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
    <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
    <Setter Property="Content" Value="Enter"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border 
                      Name="Border"
                      CornerRadius="4" 
                      Padding="10"
                      BorderBrush="#666666"
                      Background="#e5e5e5"
                      BorderThickness="1">
                  <ContentPresenter Content="{TemplateBinding Content}" />
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsFocused" Value="True">
                        <Setter TargetName="Border" Property="BorderBrush" Value="#90c74b"/>
                        <Setter TargetName="Border" Property="BitmapEffect">
                            <Setter.Value>
                                <DropShadowBitmapEffect Color="#90c74b" Direction="0" ShadowDepth="4" Opacity="0.7" Softness="0.8" />
                            </Setter.Value>
                        </Setter>
                    </Trigger>

                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34631757

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档