khukho
khukho

Reputation: 476

Need to enable the button when all textbox has value in UWP C#

I'm developing a UWP app and want to implement the feature only to enable the submit button when all required textboxes, checkboxes, etc have value.

I can only think of one idea that in all controls' changed handler, I need to write conditions that all fields have value but it is tedious and surely think there is a nice way to do this. And I found many resources for WPF but not for UWP. So I came up here.

Thank you.

Upvotes: 0

Views: 191

Answers (1)

dear_vv
dear_vv

Reputation: 2358

There are no simple way could directly achieve your requirement. You could do it through data binding. First, you need to set public string property Text1,Text2 and Boolean property Enable, Checked in code behind, then you could bind them to the corresponding property of element. So that you could set the Enable value through the property value that other controls passed to backend.

Please refer to the following.

Xaml code:

<StackPanel>
   <Button x:Name="button" Content="Load" Click="Button_Click" IsEnabled="{x:Bind Enable,Mode=TwoWay}"/>
   <TextBox x:Name="textBox1" Text="{x:Bind Text1,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
   <TextBox x:Name="textBox2"  Text="{x:Bind Text2,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
   <CheckBox IsChecked="{x:Bind Checked,Mode=TwoWay}"/>
</StackPanel> 

Code behind:

public sealed partial class MainPage : Page, INotifyPropertyChanged
{
    private bool _enable;
    private string _text1;
    private string _text2;
    private bool _checked;

    public event PropertyChangedEventHandler PropertyChanged;

    public bool Enable
    {
        get
        { return _enable;}
        set
        {
            if(value != _enable)
            {
                _enable = value;
                RaisePropertyChanged("Enable");
            }               
        }
    }
    public string Text1
    {
        get { return _text1; }
        set
        {
            _text1 = value;
            RaisePropertyChanged("Text1");
            UpdateButton();

        }
    }
    public string Text2
    {
        get { return _text2; }
        set
        {
            _text2 = value;
            RaisePropertyChanged("Text2");
            UpdateButton();
        }
    }
    public bool Checked
    {
        get { return _checked; }
        set
        {
            _checked = value;
            RaisePropertyChanged("Checked");

            UpdateButton();           
 }
   }

    private void UpdateButton()
    {
        if (Text1 != string.Empty && Text2 != string.Empty && Checked == true)
        {
            Enable = true;
        }
        else
        {
            Enable = false;
        }
    }

    public void RaisePropertyChanged(string propertyname = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyname));

    }
    
public MainPage()
    {
        this.InitializeComponent();
       
    }

   
 }

Upvotes: 2

Related Questions