如何在自定义样式的XAML和C#中创建圆形按钮?

时间:2023-01-19 16:25:44

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"];

1 个解决方案

#1


0  

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>

#1


0  

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>