Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

XF Data Trigger to Disable a Button - in code

Hi,

It's my first time using Triggers and I'm going round in circles on this one...

How do I disable a button using a DataTrigger when the length of my ActivityType field in the underlying view model is zero?

Using the code below the button never becomes disabled even though the ActivityField is empty.

Any takers?

_saveButton = new Button
{
Text = "Save Activity",
BackgroundColor = Helpers.Color.PrimaryColor.ToFormsColor(),
TextColor = Helpers.Color.TextIcons.ToFormsColor(),
Margin = 10,
};

        Binding binding = new Binding();
        binding.Source = ViewModel.ActivityType;
        binding.Path = ViewModel.ActivityType.Length.ToString();

        DataTrigger trigger = new DataTrigger(typeof(Button));
        trigger.Binding = binding;
        trigger.Value = 0;

        Setter s = new Setter();
        s.Property = IsEnabledProperty;
        s.Value = false;

        trigger.Setters.Add(s);
        _saveButton.Triggers.Add(trigger);

Cheer,
Iain

Posts

  • MichaelRumplerMichaelRumpler ATMember ✭✭✭✭✭

    Triggers are made to be used in Xaml so that you don't need to code something in the code behind. If you don't use Xaml, then it is much easier to just change the IsEnabled property in the respective PropertyChanged event handlers directly. I never saw any examples how this should work in code behind.

  • SticksSticks GBMember

    Thanks Michael - I followed your advice and it's all working

Sign In or Register to comment.