wpf 如何使用自定义样式在 XAML 和 C# 中制作圆形按钮?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36157661/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:43:40  来源:igfitidea点击:

How to make circle button in XAML and C# with customize style?

c#wpfxamlbuttongeometry

提问by Ali.Ghzd

I am new in XAML and I have a problem with my button in my simple game. I make a circle button and I need to change it's color, but my button doesn't give the default style of a button like animate, pointer change on hover it, click animate and etc, also it's color doesn't change.

我是 XAML 新手,我的简单游戏中的按钮有问题。我制作了一个圆形按钮,我需要更改它的颜色,但我的按钮没有提供按钮的默认样式,例如动画、悬停时指针更改、单击动画等,它的颜色也不会改变。

Here is my XAML:

这是我的 XAML:

<Page.Resources>
    <SolidColorBrush x:Key="RedColorXX" Color="Red" />
</Page.Resources>

<Button x:Name="btnRed" Style="{StaticResource  btnColor}" Content="Red" HorizontalAlignment="Left" Height="228" Margin="62,261,0,0" VerticalAlignment="Top" Width="228" Background="#FFCA6969" Click="colors_Click" FontSize="0.01" BorderBrush="Azure" Grid.Column="1" >
    <Button.Template >
        <ControlTemplate TargetType="Button" >
            <Grid >
                <Path Stretch="Uniform" UseLayoutRounding="False" Fill="#FFCA6969">
                    <Path.Data>
                        <EllipseGeometry RadiusX="1" RadiusY="1"/>
                    </Path.Data>
                </Path>
                <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
            </Grid>
        </ControlTemplate>
    </Button.Template>
</Button>

and the color change code that dosen't work !

和不起作用的颜色更改代码!

btnRed.Background = (SolidColorBrush)Resources["RedColorXX"];

回答by MsDeveloper

For example I turn your fill="#FFCA6969" into yellow:

例如我把你的 fill="#FFCA6969" 变成黄色:

<Button x:Name="btnRed"  Content="Red" HorizontalAlignment="Left" Height="228" Margin="100,35,0,0" VerticalAlignment="Top" Width="228" Background="#FFCA6969" Click="colors_Click" FontSize="0.01" BorderBrush="Azure" >
        <Button.Template >
            <ControlTemplate TargetType="Button" >
                <Grid >
                    <Path Stretch="Uniform" UseLayoutRounding="False" Fill="Yellow">
                        <Path.Data>
                            <EllipseGeometry RadiusX="1" RadiusY="1"/>
                        </Path.Data>
                    </Path>
                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Grid>
            </ControlTemplate>
        </Button.Template>
    </Button>