如何使用MAUI更改XAML中单选按钮的内部颜色?

tpgth1q7  于 2023-05-11  发布在  其他
关注(0)|答案(1)|浏览(278)

我希望在选择和取消选择时更改单选按钮的颜色,但是,我尝试的方法似乎已经过时。
例如:

<RadioButton Content="Add" Background="Blue" Foreground="Green" BorderBrush="Red" BorderThickness="5" />

前景似乎不再支持

<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="RadioButton">
            <Border Background="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}">
                <Path Fill="{TemplateBinding Foreground}" Data="M0,0 L0,6 L6,3 z"/>
            </Border>
        </ControlTemplate>
    </Setter.Value>
</Setter>

与第一个问题类似,不再支持Property="Template"TargetType="RadioButton"

如何在VS 2022中更改这些颜色/状态?所附的图像是我所指的按钮部分
此图像显示了我所指的按钮的部分。

yrwegjxp

yrwegjxp1#

你可以为你的单选按钮做一个ControlTemplate,就像在ContentPage中一样。进一步的调整,如改变笔划或内部椭圆的颜色是不言自明的。

<ContentPage.Resources>
    <ControlTemplate x:Key="RadioButtonTemplate">
        <Border
            BackgroundColor="#F3F2F1"
            HeightRequest="20"
            HorizontalOptions="Center"
            Stroke="#F3F2F1"
            StrokeShape="RoundRectangle 10"
            StrokeThickness="2"
            VerticalOptions="Center"
            WidthRequest="20">
            <Grid WidthRequest="20">
                <Grid
                    
                    HeightRequest="18"
                    HorizontalOptions="Center"
                    VerticalOptions="Center"
                    WidthRequest="18">
                    <Ellipse
                        Fill="White"
                        HeightRequest="15"
                        HorizontalOptions="Center"
                        Stroke="Blue"
                        VerticalOptions="Center"
                        WidthRequest="15" />
                    <Ellipse
                        x:Name="check"
                        Fill="Blue"
                        HeightRequest="8"
                        HorizontalOptions="Center"
                        VerticalOptions="Center"
                        WidthRequest="8" />
                </Grid>
                <ContentPresenter />
            </Grid>
            <VisualStateManager.VisualStateGroups>
                <VisualStateGroupList>
                    <VisualStateGroup x:Name="CheckedStates">
                        <VisualState x:Name="Checked">
                            <VisualState.Setters>
                                <Setter Property="Stroke" Value="#FF3300" />
                                <Setter TargetName="check" Property="Opacity" Value="1" />
                            </VisualState.Setters>
                        </VisualState>
                        <VisualState x:Name="Unchecked">
                            <VisualState.Setters>
                                <Setter Property="BackgroundColor" Value="#F3F2F1" />
                                <Setter Property="Stroke" Value="#F3F2F1" />
                                <Setter TargetName="check" Property="Opacity" Value="0" />
                            </VisualState.Setters>
                        </VisualState>
                    </VisualStateGroup>
                </VisualStateGroupList>
            </VisualStateManager.VisualStateGroups>
        </Border>
    </ControlTemplate>
    <Style TargetType="RadioButton">
        <Setter Property="ControlTemplate" Value="{StaticResource RadioButtonTemplate}" />
    </Style>

</ContentPage.Resources>

相关问题