Change TextColor & Use Command on IsEnabled in Button in Xamarin

廉价感情. 提交于 2021-01-07 02:43:09

问题


I'm using IsEnabled on Button in Xamarin App and there is 2 things I can't do.

  1. Change the TextColor when the IsEnabled = false, but I can change the BackgroundColor.

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?

  1. I want to enable the Command only when the IsEnabled = true e.g. the ICommand in ViewModel should only works, when the IsEnabled value is true.

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:


  1. 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.

  1. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!