Issue
I'm using IsEnabled on Button in Xamarin App and there is 2 things I can't do.
- Change the
TextColorwhen theIsEnabled = false, but I can change theBackgroundColor.
The solution is to use the Custom Entry and there is great article for that => https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/custom-renderer/entry
But it only works with:
public class MyEntry : Entry
{
}
and the code behind my Page is:
public class MyEntry : ContentPage
{
}
and also I can't use multiple classes. Is there any way to use the Entry with ContentPage in xml.cs Page?
- I want to enable the
Commandonly when theIsEnabled = truee.g. theICommandin ViewModel should only works, when theIsEnabledvalue istrue.
Complete Code Sample => https://stackoverflow.com/a/64808306/14139029
.xml
<Button
x:Name="PasswordButton"
IsEnabled="False"
TextColor="#4DABFE"
BackgroundColor = "#FFFFFF"
Text="Submit"
Command={Binding PasswordButtonCommand}>
</Button>
.xml.cs
if (Password.Text == ConfirmPassword.Text)
{
PasswordButton.IsEnabled = true;
PasswordButton.TextColor = Color.FromHex("004B87");
PasswordButton.BackgroundColor = Color.FromHex("222222");
}
Solution
As when you set the Button IsEnabled="False",it will use a default style of the button.
If you want to use your custom style and handle the Command when IsEnable is true.you could define a custom property in your viewmodel,and then trigger your Command based on this property.
For example:
public class ViewModel
{
public ICommand PasswordButtonCommand => new Command<Button>(Submit);
public bool IsEnable { get; set; }
private void Submit(object obj)
{
if (IsEnable)
{
//do something you want
}
}
}
then in your Entry TextChanged event :
private void Entry_TextChanged(object sender, TextChangedEventArgs e)
{
if (Password.Text == ConfirmPassword.Text)
{
viewModel.IsEnable = true;
PasswordButton.TextColor = Color.FromHex("004B87");
PasswordButton.BackgroundColor = Color.FromHex("222222");
}
else
{
viewModel.IsEnable = false;
PasswordButton.TextColor = Color.FromHex("4DABFE");
PasswordButton.BackgroundColor = Color.FromHex("FFFFFF");
}
}
Answered By - Leo Zhu - MSFT
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.