C# WPF-按钮';s内容没有';t显示

C# WPF-按钮';s内容没有';t显示,c#,wpf,button,styles,resourcedictionary,C#,Wpf,Button,Styles,Resourcedictionary,这是我第一次使用资源字典来设置UI控件的样式。嗯,我在显示按钮中的文本时遇到了一个问题,也许问题是显示按钮我不知道出了什么问题。我将资源字典添加到App.xalm。在设计视图中,看起来一切正常-按钮中没有文本 我试过了: 这两个变量都具有内容属性,并在按钮的打开和关闭标记之间添加文本 更改样式中的高度和宽度特性 资源字典: <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentati

这是我第一次使用资源字典来设置UI控件的样式。嗯,我在显示按钮中的文本时遇到了一个问题,也许问题是显示按钮我不知道出了什么问题。我将资源字典添加到App.xalm。在设计视图中,看起来一切正常-按钮中没有文本

我试过了

  • 这两个变量都具有内容属性,并在按钮的打开和关闭标记之间添加文本

  • 更改样式中的高度和宽度特性

资源字典:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:local="clr-namespace:CyclingTrainingPlan">

<Style x:Key="buttonStyle" TargetType="{x:Type Button}">
    <Setter Property="Foreground" Value="#b8c7ce" />
    <Setter Property="Background" Value="Transparent" />
    <Setter Property="BorderBrush" Value="Transparent" />
    <Setter Property="Height" Value="Auto" />
    <Setter Property="Width" Value="Auto" />
    <Setter Property="Margin" Value="20" />
    <Setter Property="Control.Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background" Value="#000" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

哈姆:


解决方案:

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

<!-- Properties... -->

<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" Value="#1a2226"/>
        </Trigger>
    </Style.Triggers>

我在这里对它进行了测试,它按照预期完美工作

也许问题在于这种风格的关联。 试着从

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

<!-- Properties... -->

<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" Value="#1a2226"/>
        </Trigger>
    </Style.Triggers>