WPF按钮自定义模板背景随Horizo​​ntalAlignment更改

时间:2015-01-20 16:05:00

标签: c# wpf controltemplate

我的应用程序中有一个奇怪的行为。我使用controlTemplating定义了自定义按钮,如下所示

<Style x:Key="StandardButton" TargetType="Button">
    <Setter Property="Visibility" Value="Visible"/>
    <Setter Property="Foreground" Value="#FFFFFF"/>
    <Setter Property="Background" Value="#3F3F46"/>
    <Setter Property="BorderBrush" Value="#54545C"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border BorderThickness="2"
                        BorderBrush="{TemplateBinding BorderBrush}" 
                        Background="{TemplateBinding Background}">
                    <TextBlock HorizontalAlignment="Center"><ContentPresenter /></TextBlock>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

在设计师中看起来像这样 Button in designer

然而,当我运行我的应用程序时,它看起来更像是这样 Button in application

你看到按钮中文字周围的较暗部分?当我没有指定textbock的Horizo​​ntalAlignment属性时,我没有这种行为。这是我的屏幕问题还是我忘记了什么? 谢谢!

修改

<Window x:Class="MyApp.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Background="#FF2D2D30">
    <Grid>
        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="222,151,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="buttonClBck" />
     </Grid>
</Window>

<Application x:Class="OtdrQualifTools.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/ControlStyles;component/Styles.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

1 个答案:

答案 0 :(得分:0)

如果没有看到你正在使用该风格的xaml,那将很难说。它对我来说很好。根据您的要求,我不确定为什么您要将内容呈现器包装在TextBlock中。见下文。

<Window.Resources>
    <Style x:Key="StandardButton" TargetType="Button">
        <Setter Property="Visibility" Value="Visible"/>
        <Setter Property="Foreground" Value="#FFFFFF"/>
        <Setter Property="Background" Value="#3F3F46"/>
        <Setter Property="BorderBrush" Value="#54545C"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border BorderThickness="2"
                    BorderBrush="{TemplateBinding BorderBrush}" 
                    Background="{TemplateBinding Background}">
                        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
<Grid>
    <Button Style="{StaticResource StandardButton}" Height="30" Width="100" Content="Button" />
</Grid>

对于它的价值......你的app.xaml中可能会有一个流氓风格,它将在运行时显示,但不会在主窗口的设计器中显示。这样的事情(我能够以这种方式再现你所看到的):

<Style TargetType="TextBlock">
        <Setter Property="Background" Value="#FF343339" />
    </Style>