根据启用值更改 Maui Android 中按钮的颜色文本

问题描述 投票:0回答:1

我在 Maui Android 的弹出窗口中有一个按钮,它具有到

IsEnabled
属性的绑定。我创建了一个转换器,以便在
IsSignButtonEnabled
属性为 1 或 0 时更改文本颜色。

随着属性值的变化,转换器可以正确运行。但按钮文本仅在启用时才会改变颜色。如果它被禁用,它不会选择我在转换器中的颜色。但是程序确实沿着选择一种颜色或另一种颜色的路径...除此之外,

IsEnabled
属性起作用,因为它禁用了按钮...我不明白...

<Button
Grid.Row="2"
Text="Firmar"  
IsEnabled="{Binding IsSignButtonEnabled}"
TextColor="{Binding IsSignButtonEnabled, Converter={StaticResource ButtonColorConverter}}"  
Clicked="OpenCart"/>
public class ButtonColorConverter : IValueConverter
{
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (value is bool isEnabled && !isEnabled)
{
return Color.FromHex("#f5092b"); // Color para botón deshabilitado
}
else
{
return Color.FromHex("#04b84a"); // Color para botón habilitado
}
}

    public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }

}

ViewModel
我有
ObservableProperty
可以完美工作

[ObservableProperty]
public bool isSignButtonEnabled;

我需要按钮从一种颜色更改为另一种颜色,具体取决于它是否启用。

android button binding maui converters
1个回答
0
投票

我认为在您的情况下使用转换器是一种矫枉过正,因为您可以只使用 VisualStateManager

<Button Grid.Row="2" Text="Firmar" Clicked="OpenCart"
IsEnabled="{Binding IsSignButtonEnabled}">
                <VisualStateManager.VisualStateGroups>
                    <VisualStateGroupList>
                        <VisualStateGroup Name="CommonStates">
                            <VisualState Name="Normal">
                                <VisualState.Setters>
                                    <Setter Property="TextColor" Value="#04b84a" />
                                </VisualState.Setters>
                            </VisualState>

                            <VisualState Name="Disabled">
                                <VisualState.Setters>
                                    <Setter Property="TextColor" Value="#f5092b" />
                                </VisualState.Setters>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateGroupList>
                </VisualStateManager.VisualStateGroups>
</Button>
© www.soinside.com 2019 - 2024. All rights reserved.