在XAML

时间:2016-01-06 11:14:04

标签: c# wpf xaml

我为我的表单写了一个样式,一切似乎工作得很好,除了我没有在我的按钮上得到一个文本。我唯一得到的是没有内容的灰色按钮。

以下是我的尝试:

<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”。

1 个答案:

答案 0 :(得分:0)

您需要在模板中使用ContentPresenter才能显示该文字。

试试这个:

<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>