如何使用WPF在XAML中更改单个按钮的背景和悬停颜色?

628mspwn  于 2023-09-28  发布在  其他
关注(0)|答案(1)|浏览(127)

这是我的表格:

我试图改变悬停背景颜色和悬停文本前景色的“浏览”按钮。
这是我的XAML:

<Window x:Class="Base64ToFileWPF.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Base64ToFileWPF"
        mc:Ignorable="d"
        Title="MainWindow" Width="800" SizeToContent="Height" ResizeMode="NoResize" WindowStartupLocation="CenterScreen">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <StackPanel Grid.Row="0">
            <Label Name="lblQuestion" Margin="15,10,0,0" FontSize="22" VerticalContentAlignment="Center" HorizontalAlignment="Left" Height="45" Foreground="#FF2641D8">Base64 String to File</Label>
            <Label Name="subQuestion" Margin="16,0,0,10" FontSize="16" VerticalContentAlignment="Center" HorizontalAlignment="Left" Height="33" Foreground="#FF676D92">Please enter your Base64 string below:</Label>
        </StackPanel>

        <StackPanel Grid.Row="1">
            <RichTextBox Height="270" FontFamily="Input Mono" FontSize="15px" Margin="23,0,23,0" Padding="8" VerticalScrollBarVisibility="Visible" Foreground="#FFA2A4A9">
                <FlowDocument>
                    <Paragraph LineHeight="24px">
                        <Run Text="PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEwMDEgMTAwMSAxMDAxIj4NCgk8cGF0aCBmaWxsPSIjMDAwMDAwIiBkPSJNOTkyIC0yNDBRMTAxMCAtMjMxIDk5MiAtMjI1TDUxOCAtN1E1MDAgMiA0ODMgLTdMOSAtMjI1US05IC0yMzEgOSAtMjQwTDEyMiAtMjkxUTE0MCAtMzAwIDE1OCAtMjkxTDQ4MyAtMTQyUTUwMSAtMTMzIDUxOCAtMTQyTDg0MyAtMjkxUTg2MSAtMzAwIDg3OSAtMjkxWk05OTIgLTUwOVExMDEwIC01MDAgOTkyIC00OTJMNTE4IC0yNzRRNTAwIC0yNjcgNDgzIC0yNzRMOSAtNDkyUS05IC01MDEgOSAtNTA5TDEyMiAtNTYxUTE0MCAtNTY5IDE1OCAtNTYxTDQ4MyAtNDEyUTUwMSAtNDAzIDUxOCAtNDEyTDg0MyAtNTYxUTg2MSAtNTY5IDg3OSAtNTYxWk05IC03NjFRLTkgLTc3MCA5IC03NzZMNDgzIC05OTRRNTAxIC0xMDAzIDUxOCAtOTk0TDk5MiAtNzc2UTEwMTAgLTc3MCA5OTIgLTc2MUw1MTggLTU0M1E1MDAgLTUzNCA0ODMgLTU0M1oiLz4NCjwvc3ZnPg=="/>
                    </Paragraph>
                </FlowDocument>
            </RichTextBox>
        </StackPanel>

        <StackPanel Orientation = "Horizontal" Grid.Row="2" HorizontalAlignment="Left">
            <Button Content="Browse" Height="30" Width="100" Margin="23,15,15,15" FontFamily="Helvetica Now Text" FontSize="14" Foreground="#777777">
                <Button.Style>
                    <Style TargetType="Button">
                        <Setter Property="Background" Value="Green"/>
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="Button">
                                    <Border Background="#FFFFFF" BorderBrush="#AAAAAA" BorderThickness="1" CornerRadius="2">
                                        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                    </Border>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                        <Style.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="Background" Value="#FF0000"/>
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </Button.Style>
            </Button>
            <TextBox Text="Enter your full filename here." Height="30" Width="400" Padding="10,0,0,1" VerticalContentAlignment="Center" FontFamily="Helvetica Now Text" FontSize="13px" Foreground="#FF4E4E4E" />
        </StackPanel>

        <StackPanel Orientation = "Horizontal" Grid.Row="2" HorizontalAlignment="Right">
            <Button Content="Cancel" Height="30" Width="100" Margin="23,15,15,15" ></Button>
            <Button Content="Create File" Height="30" Width="100" Margin="0,15,23,15" ></Button>
        </StackPanel>

    </Grid>
</Window>

尽管设置了<Button.Style>,但什么都不工作。按钮保持白色。我已经尝试过在样式定义上做些手脚,但我就是不知道如何做到这一点。
我做错了什么?

nfs0ujit

nfs0ujit1#

您正在使用Border来呈现按钮背景,它的Background属性设置为常量值#FFFFFF,因此它始终保持白色。当ControlTemplate元素没有引用控件属性时,控件属性没有任何作用。
使用TemplateBinding引用模板中的控件属性:

Border Background="{TemplateBinding Background}"

相关问题