问题
I'm using IsEnabled
on Button in Xamarin App and there is 2 things I can't do.
- Change the
TextColor
when 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
Command
only when theIsEnabled = true
e.g. theICommand
in ViewModel should only works, when theIsEnabled
value 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");
}
回答1:
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");
}
}
回答2:
- Change the TextColor when the IsEnabled = false, but I can change the BackgroundColor. Why?
Yes, you can change the BackgroundColor but not the TextColor becuase it overrides internally. You will have to create renderer to change the text color.
- When I use Command, the IsEnabled becomes true. The IsEnabled didn't work then. Code for IsEnabled is written in .xml.cs page. Is there any possibility to use Command with IsEnabled?
There is no relation between command and IsEnabled property so it should work.
Xaml.cs
<Button
x:Name="PasswordButton"
Clicked="PasswordButton_Clicked"
IsEnabled="False" Command="{Binding PasswordButtonCommand}"
Text="Submit">
</Button>
ViewModel.cs
public class ViewModel : INotifyPropertyChanged
{
public ICommand PasswordButtonCommand => new Command(Submit);
private void Submit(object obj)
{
Debug.WriteLine("Test");
}
public ViewModel()
{
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(
[CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this,
new PropertyChangedEventArgs(propertyName));
}
}
来源:https://stackoverflow.com/questions/64820270/change-textcolor-use-command-on-isenabled-in-button-in-xamarin