Uwp 是否可以将禁用按钮的颜色更改为与背景面板相同的颜色?

Uwp 是否可以将禁用按钮的颜色更改为与背景面板相同的颜色?,uwp,Uwp,是否可以将禁用按钮的颜色更改为与背景堆栈面板相同的颜色 我尝试了几种方法,但仍然无法使其起作用 欢迎发表评论您可以定义自定义的控制模板: <Button x:Name="stopButton_" Background="#D3D3D3" BorderThickness="0.3" BorderBrush="Black" IsEnabled="False" Height="48&q

是否可以将禁用按钮的颜色更改为与背景堆栈面板相同的颜色

我尝试了几种方法,但仍然无法使其起作用


欢迎发表评论

您可以定义自定义的
控制模板

<Button x:Name="stopButton_" Background="#D3D3D3" BorderThickness="0.3" BorderBrush="Black" IsEnabled="False" Height="48" Width="96" Margin="5.5">
    <Button.Template>
        <ControlTemplate TargetType="Button">
            <ContentPresenter x:Name="ContentPresenter"
              Background="{TemplateBinding Background}"
              BackgroundSizing="{TemplateBinding BackgroundSizing}"
              BorderBrush="{TemplateBinding BorderBrush}"
              BorderThickness="{TemplateBinding BorderThickness}"
              Content="{TemplateBinding Content}"
              ContentTemplate="{TemplateBinding ContentTemplate}"
              ContentTransitions="{TemplateBinding ContentTransitions}"
              CornerRadius="{TemplateBinding CornerRadius}"
              Padding="{TemplateBinding Padding}"
              HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
              VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
              AutomationProperties.AccessibilityView="Raw">

                <VisualStateManager.VisualStateGroups>
                    <VisualStateGroup x:Name="CommonStates">
                        <VisualState x:Name="Normal">

                            <Storyboard>
                                <PointerUpThemeAnimation Storyboard.TargetName="ContentPresenter" />
                            </Storyboard>
                        </VisualState>

                        <VisualState x:Name="PointerOver">

                            <Storyboard>
                                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Background">
                                    <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonBackgroundPointerOver}" />
                                </ObjectAnimationUsingKeyFrames>
                                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="BorderBrush">
                                    <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonBorderBrushPointerOver}" />
                                </ObjectAnimationUsingKeyFrames>
                                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground">
                                    <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonForegroundPointerOver}" />
                                </ObjectAnimationUsingKeyFrames>
                                <PointerUpThemeAnimation Storyboard.TargetName="ContentPresenter" />
                            </Storyboard>
                        </VisualState>

                        <VisualState x:Name="Pressed">

                            <Storyboard>
                                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Background">
                                    <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonBackgroundPressed}" />
                                </ObjectAnimationUsingKeyFrames>
                                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="BorderBrush">
                                    <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonBorderBrushPressed}" />
                                </ObjectAnimationUsingKeyFrames>
                                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground">
                                    <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonForegroundPressed}" />
                                </ObjectAnimationUsingKeyFrames>
                                <PointerDownThemeAnimation Storyboard.TargetName="ContentPresenter" />
                            </Storyboard>
                        </VisualState>

                        <VisualState x:Name="Disabled">

                            <Storyboard>
                                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Background">
                                    <DiscreteObjectKeyFrame KeyTime="0" Value="#D3D3D3" />
                                </ObjectAnimationUsingKeyFrames>
                                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="BorderBrush">
                                    <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonBorderBrushDisabled}" />
                                </ObjectAnimationUsingKeyFrames>
                                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground">
                                    <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonForegroundDisabled}" />
                                </ObjectAnimationUsingKeyFrames>
                            </Storyboard>
                        </VisualState>

                    </VisualStateGroup>

                </VisualStateManager.VisualStateGroups>
            </ContentPresenter>

        </ControlTemplate>
    </Button.Template>
</Button>

在“已禁用”的VisualState中更改背景

是否可以将禁用按钮的颜色更改为与背景面板相同的颜色

对于这种情况,您可以检查按钮的样式并找到
ButtonBackgroundDisabled
StaticResource,如果我们设置buttondisabled,按钮将加载
ButtonBackgroundDisabled
color

因此,我们只需要制作一个与stackpanel相同的按钮BackgroundDisabled
SolidColorBrush
,如下所示

<Page.Resources>   
    <Color x:Key="PanelBackground">#F0F078D0</Color>
    <SolidColorBrush x:Key="ButtonBackgroundDisabled" Color="{StaticResource PanelBackground}" />
</Page.Resources>
<Grid x:Name="RootGrid" Background="{StaticResource PanelBackground}">
    <Button
        VerticalAlignment="Center" IsEnabled="False" 
        Click="Button_Click"
        Content="Click"
        Visibility="Visible" />
</Grid>

#F078D0

新答案有效!