Forum Xamarin.Forms


The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

Trouble creating a DataTrigger in C# (not XAML)

TimFTimF USMember ✭✭

I'm having trouble creating a DataTrigger in C# (the project I'm working on isn't using XAML).

I'm trying to disable a button when the text length of a field is 0.

Here's the code:

        Entry emailEntry = new Entry
            Placeholder = "email",
            Text = "",
            HorizontalOptions = LayoutOptions.Fill,
        Button signInButton = new Button
            Text = "sign in",
            HorizontalOptions = LayoutOptions.CenterAndExpand
    DataTrigger trigger = new DataTrigger(typeof(Button));
        Binding b = new Binding();
        b.Source = emailEntry;
        b.Path = "Text.Length";
        trigger.Binding = b;
        trigger.Value = "0";
        Setter s = new Setter();
        s.Property = Button.IsEnabledProperty;
        s.Value = false;

I tried making a test project with some simple XAML in it to set up this scenario (it works in XAML), and it looks like the trigger has the same values (via watch window) in both projects.

Any ideas?


  • AdwaitMoreAdwaitMore USMember
    edited August 2016

    Hey @TimF,
    your code is perfectly correct except for the value that you are setting for trigger.Value. Set it to a non-string.
    Just make the following changes

  • AdwaitMoreAdwaitMore USMember
    edited August 2016

Sign In or Register to comment.