按钮不透明度不适用

时间:2018-01-22 09:02:34

标签: wpf xaml button opacity

所以我想制作一个按钮模板。但是,我不能让后台不透明工作,尽管它实现的方式与父容器(它工作的地方)完全相同。如果我从模板中完全删除了背景设置器,那么它正确地从父容器继承,但是只有按钮的文本是可点击的(而不是周围环境),所以看来我需要设置背景按钮本身。

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

我的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>

1 个答案:

答案 0 :(得分:0)

我实际上找到了一个解决方案。这很愚蠢,但它确实有效。

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

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

通过具有透明背景,整个按钮变为可单击,但按钮的实际颜色(和不透明度)将从父容器继承。