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:
</p> <pre><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; trigger.Setters.Add(s); signInButton.Triggers.Add(trigger);

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?

Answers

  • 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
    trigger.Value=0

  • AdwaitMoreAdwaitMore USMember
    edited August 2016

Sign In or Register to comment.