Issue
In Xamarin App, I want to change the TextColor
of Button
when the IsEnabled = false
. This button won't be enabled until on Item is selected from the Picker
.
There is a solution by @Leo Zhu - MSFT => https://stackoverflow.com/a/64854260/14139029, which is very helpful, but it isn't working in this situation.
.xml
<Button
x:Name="FormButton"
IsEnabled="False"
TextColor="#4DABFE"
Text="Submit"
Command={Binding FormButtonCommand}>
<Button.Triggers>
<MultiTrigger TargetType="Button">
<MultiTrigger.Conditions>
<BindingCondition Binding="{Binding SelectedState,
Converter={StaticResource stringNullOrEmptyValueBoolConverter }}" Value="false" />
<BindingCondition Binding="{Binding SelectedCity,
Converter={StaticResource stringNullOrEmptyValueBoolConverter }}" Value="false" />
</MultiTrigger.Conditions>
<Setter Property="IsEnabled" Value="True" />
</MultiTrigger>
</Button.Triggers>
</Button>
ViewModel.cs
public ICommand FormButtonCommand => new Command(async (formbutton) =>
{
if (SelectedCity == "Tokyo")
{
// Code
}
else if (SelectedCity == "Moscow")
{
// Code
}
}, (x) => false);
Solution
Ok,it's the same reason,you set the Button's IsEnable
property,when it's false,it will display with a default style.
Here is a solution for this case(it also applies to the last case):
Create a custom ButtonRenderer in both platform:
For Android:
[assembly: ExportRenderer(typeof(Button), typeof(CustomButtonRenderer))]
namespace Your namepace
{
class CustomButtonRenderer :ButtonRenderer
{
public CustomButtonRenderer(Context context):base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e)
{
base.OnElementChanged(e);
if (Control != null) SetColors();
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs args)
{
base.OnElementPropertyChanged(sender, args);
if (args.PropertyName == nameof(Button.IsEnabled)) SetColors();
}
private void SetColors()
{
Control.SetTextColor(Element.IsEnabled ? Color.FromHex("004B87").ToAndroid() : Color.FromHex("4DABFE").ToAndroid());
Control.SetBackgroundColor(Element.IsEnabled ? Color.FromHex("222222").ToAndroid() : Color.FromHex("FFFFFF").ToAndroid());
}
}
}
For ios:
[assembly: ExportRenderer(typeof(Button), typeof(CustomButtonRenderer))]
namespace Your namepace
{
public class CustomButtonRenderer: ButtonRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Button> args)
{
base.OnElementChanged(args);
if (Control != null) SetColors();
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs args)
{
base.OnElementPropertyChanged(sender, args);
if (args.PropertyName == nameof(Button.IsEnabled)) SetColors();
}
private void SetColors()
{
Control.SetTitleColor(Element.IsEnabled ? Color.FromHex("004B87").ToUIColor() : Color.FromHex("4DABFE").ToUIColor(), Element.IsEnabled ? UIControlState.Normal : UIControlState.Disabled);
Control.BackgroundColor = Element.IsEnabled ? Color.FromHex("222222").ToUIColor() : Color.FromHex("FFFFFF").ToUIColor();
}
}
}
Answered By - Leo Zhu - MSFT
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.