如何在按下时删除按钮的背景颜色 - Xamarin 表单 UWP



我想在按下按钮时删除按钮背景颜色,并且由于我使用图像作为背景,因此喜欢透明背景。
能够在鼠标悬停时通过以下代码删除边框:

btn.BorderThickness = new Thickness(0,0,0,0);
btn.Padding = new Thickness(0, 0, 0, 0);

并通过设置:

btn.Background = new Windows.UI.Xaml.Media.SolidColorBrush(Windows.UI.Colors.Transparent);

但是,单击它时我无法删除背景,它显示灰色背景。
任何建议将不胜感激。

无论您设置什么,由于CommonStates VisualStateGroupPressed状态,默认模板都将(应该)覆盖您的值。

<VisualState x:Name="Pressed">
    <Storyboard>
        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="RootGrid"
                                       Storyboard.TargetProperty="Background">
            <!-- This will cause the gray background color of the button -->
            <DiscreteObjectKeyFrame
                KeyTime="0"
                Value="{ThemeResource SystemControlBackgroundBaseMediumLowBrush}" />
        </ObjectAnimationUsingKeyFrames>
    </Storyboard>
</VisualState>

与其从代码隐藏中设置颜色 - 以及边框,您现在如何做 - 您应该为按钮创建自己的模板,将Background设置为所需的值,例如 Transparent .

最新更新