按钮不透明度不适用

Button opacity does not apply

所以我正在尝试制作一个按钮模板。但是,我无法使背景不透明度起作用,尽管它的实现方式与父容器(它起作用的地方)完全相同。如果我从模板中完全删除背景 setter,那么它会正确地从父容器继承,但是只有按钮的文本是可点击的(而不是周围的文字),所以看来我需要设置按钮本身的背景。

如何使按钮的背景与父控件的背景相同,同时又不会失去单击整个按钮(而不仅仅是文本)的能力?

我的 XAML 看起来像这样:

<Grid DockPanel.Dock="Top">
    <!-- This is what I also want for my buttons-->
    <Grid.Background>
        <SolidColorBrush Color="Black" Opacity="0.8" />
    </Grid.Background>

    <UniformGrid 
        HorizontalAlignment="Center"
        Columns="4"
        Width="800"
        Height="50">

        <UniformGrid.Resources>
            <Style TargetType="Button">
                <Setter Property="FontFamily" Value="Segoe UI" />
                <Setter Property="FontSize" Value="12" />

                <!-- And this is how I try to achieve it -->
                <Setter Property="Background">
                    <Setter.Value>
                        <SolidColorBrush Color="Black" Opacity="0.8" />
                    </Setter.Value>
                </Setter>
                <Setter Property="FontWeight" Value="Medium" />
                <Setter Property="Foreground" Value="White" />
                <Setter Property="BorderThickness" Value="0" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="Button">
                            <Border BorderThickness="0" Background="{TemplateBinding Background}">
                                <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                                <Border.Style>
                                    <Style TargetType="{x:Type Border}">
                                        <Style.Triggers>
                                            <Trigger Property="IsMouseOver" Value="True">
                                                <Setter Property="TextBlock.Foreground" Value="Silver"/>
                                            </Trigger>
                                        </Style.Triggers>
                                    </Style>
                                </Border.Style>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </UniformGrid.Resources>

        <Button 
            Content="{Binding ValuationButtonText}" 
            Command="{Binding NavigationCommand}"
            CommandParameter="valuation"
            Foreground="{Binding ValuationButtonForeground}" />

        <Button 
            Content="Opdater Leasing Core" 
            Command="{Binding NavigationCommand}"
            CommandParameter="leasingCore"
            Foreground="{Binding LeasingCoreButtonForeground}" />

        <Button 
            Content="Opdater Motorregister" 
            Command="{Binding NavigationCommand}"
            CommandParameter="motorregister"
            Foreground="{Binding MotorregisterButtonForeground}" />

        <Button
            Content="Behandles Manuelt"
            Command="{Binding NavigationCommand}"
            CommandParameter="manualProcessing"
            Foreground="{Binding ManualProcessingButtonForeground}" />

    </UniformGrid>

</Grid>

我居然找到了解决办法。这很愚蠢,但它确实有效。

您将 <Setter> 背景属性替换为:

<Setter Property="Background">
    <Setter.Value>
        <SolidColorBrush Color="Transparent" Opacity="1" />
    </Setter.Value>
</Setter>

通过透明背景,整个按钮都可以点击,但按钮的实际颜色(和不透明度)是从父容器继承的。