<Style TargetType="RadioButton" x:Key="WizardRadioButton">
<Setter Property="FontFamily" Value="Inter-Bold"/>
<Setter Property="FontSize" Value="12"/>
<Setter Property="Margin" Value="0,6,0,6"/>
<Setter Property="MinimumHeightRequest" Value="44"/>
<Setter Property="MinimumWidthRequest" Value="44"/>
<Setter Property="TextColor" Value="{AppThemeBinding Light={StaticResource Black}, Dark={StaticResource White}}"/>
<Setter Property="BackgroundColor" Value="Transparent"/>
<Setter Property="ControlTemplate">
<Setter.Value>
<ControlTemplate>
<Border StrokeThickness="0"
Stroke="Transparent"
Padding="10,5"
BackgroundColor="Transparent"
StrokeShape="RoundRectangle 8">
<ContentPresenter VerticalOptions="Center"
HorizontalOptions="Center"/>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CheckedStates">
<VisualState x:Name="Checked">
<VisualState.Setters>
<Setter Property="BackgroundColor" Value="#1CB41C"/>
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Unchecked">
<VisualState.Setters>
<Setter Property="BackgroundColor" Value="White"/>
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
您的问题始于
“我试图自定义.net maui中的radiobutton的外观”。但是,经过仔细阅读后,很明显您正在尝试自定义它,以便它看起来不像是收音机按钮了。因此,现在我们可能会有一个问题,因为(如果我关注您的帖子)您想要一个看起来像一个按钮的东西(
RadioButton
行为的方式(“一个热”)。
我要建议的是继承Button
来制作OneHotButton
,我们要做的第一件事就是使它以RadioButton
的方式行事。下面的摘要显示了如何制作可以在XAML中设置以使此工作的BindableProperty
。
GroupName
现在您需要做的就是为颜色制作可绑定的属性,例如
[DebuggerDisplay("{Text}")]
public partial class OneHotButton : Button
{
public OneHotButton()
{
Clicked += OnButtonClicked;
UpdateColors();
}
private static readonly Dictionary<string, List<OneHotButton>> buttonGroups = new();
public static readonly BindableProperty GroupNameProperty =
BindableProperty.Create(
nameof(GroupName),
typeof(string),
typeof(OneHotButton),
default(string),
propertyChanged: (bindable, oldValue, newValue) =>
{
if (bindable is OneHotButton button)
{
if (oldValue is string oldGroup)
{
buttonGroups[oldGroup].Remove(button);
if (buttonGroups[oldGroup].Count == 0)
buttonGroups.Remove(oldGroup);
}
if (newValue is string newGroup && !string.IsNullOrWhiteSpace(newGroup))
{
if (!buttonGroups.ContainsKey(newGroup))
buttonGroups[newGroup] = new List<OneHotButton>();
buttonGroups[newGroup].Add(button);
}
}
});
public static readonly BindableProperty IsCheckedProperty =
BindableProperty.Create(
nameof(IsChecked),
typeof(bool),
typeof(OneHotButton),
false,
propertyChanged: (bindable, oldValue, newValue) =>
{
if (bindable is OneHotButton button &&
Equals(newValue, true) &&
!string.IsNullOrWhiteSpace(button.GroupName) &&
buttonGroups.TryGetValue(button.GroupName, out var buttonList))
{
button.UpdateColors();
buttonList
.Where(_ => !ReferenceEquals(_, button))
.ToList()
.ForEach(_ =>
{
_.IsChecked = false;
_.UpdateColors();
});
}
});
...
}
,SelectedTextColor
等。
UnselectedTextColor
XAML示例
public Color SelectedTextColor
{
get => (Color)GetValue(SelectedTextColorProperty);
set => SetValue(SelectedTextColorProperty, value);
}
public static readonly BindableProperty SelectedBackgroundColorProperty =
BindableProperty.Create(
propertyName: nameof(OneHotButton.SelectedBackgroundColor),
returnType: typeof(Color),
declaringType: typeof(OneHotButton),
defaultValue: Colors.CornflowerBlue,
defaultBindingMode: BindingMode.OneWay,
propertyChanged: (bindable, oldValue, newValue) =>
{
if (bindable is OneHotButton @this)
{
@this.UpdateColors();
}
});
private void UpdateColors()
{
if(IsChecked)
{
TextColor = SelectedTextColor;
BackgroundColor = SelectedBackgroundColor;
}
else
{
TextColor = UnselectedTextColor;
BackgroundColor = UnselectedBackgroundColor;
}
}
... Other bindable colors
似乎是一个。因此,您可以在
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:style_radio_group"
x:Class="style_radio_group.MainPage">
<ContentPage.Resources>
<Style TargetType="local:OneHotButton" >
<Setter Property="SelectedBackgroundColor" Value="CornflowerBlue"/>
<Setter Property="Margin" Value="25,0"/>
</Style>
</ContentPage.Resources>
<ScrollView>
<VerticalStackLayout Padding="30,0" Spacing="25">
<Image
Source="dotnet_bot.png"
HeightRequest="185"
Aspect="AspectFit"
SemanticProperties.Description="dot net bot in a race car number eight" />
<Border Padding="0,0,0,10" StrokeShape="RoundRectangle 10">
<VerticalStackLayout Spacing="10">
<Label
Text="Select a Fruit:"
Padding="5"
BackgroundColor="LightGray"
FontSize="16"
FontAttributes="Bold"
HorizontalOptions="Fill"/>
<local:OneHotButton
Text="Apple"
SelectedTextColor="Salmon"
GroupName="OptionsGroup"
IsChecked="true"/>
<local:OneHotButton
Text="Banana"
SelectedTextColor="Yellow"
GroupName="OptionsGroup"/>
<local:OneHotButton
Text="Grape"
SelectedTextColor="LightGreen"
GroupName="OptionsGroup"/>
</VerticalStackLayout>
</Border>
</VerticalStackLayout>
</ScrollView>
</ContentPage>
ContentPresenter
以绑定到Label
: