C#WPF鼠标悬停按钮 - 没有内容

时间:2017-02-22 10:32:49

标签: c# wpf xaml button

我刚编辑了这个按钮。 因为我想要鼠标悬停效果。 不幸的是,Button的内容没有显示。 如果我用鼠标移动它,我想改变字体颜色。

<Button Content="Button" HorizontalAlignment="Left" VerticalAlignment="Bottom" Width="185" Height="50" HorizontalContentAlignment="Left" BorderBrush="{x:Null}" Foreground="{x:Null}" Canvas.Top="42">
    <Button.Style>
        <Style TargetType="{x:Type Button}">
            <Setter Property="Background" Value="#FF37424A"/>
            <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="#FF303B43"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
</Button>

1 个答案:

答案 0 :(得分:3)

  

不幸的是,Button的内容没有显示。

不要将Foreground属性设置为{x:Null}

  

如果我用鼠标移动它,我想改变字体颜色。

添加一个setter,用于设置Foreground触发器中的IsMouseOver属性:

<Button Content="Button" HorizontalAlignment="Left" VerticalAlignment="Bottom" Width="185" Height="50" 
                HorizontalContentAlignment="Left" BorderBrush="{x:Null}" Canvas.Top="42">
    <Button.Style>
        <Style TargetType="{x:Type Button}">
            <Setter Property="Background" Value="#FF37424A"/>
            <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="#FF303B43"/>
                    <Setter Property="Foreground" Value="Red"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
</Button>