Evyatar
Evyatar

Reputation: 1157

Clean GUI Text WPF

I'm using WPF.
I have a simple GUI with 2 text boxes, 2 radio buttons.
I want set the GUI components to default (textboxs with empty text and radio buttons unchecked).
How can i do it?
Thanks.

Explain:
I receive every 1 seconds tcp message.
If the header is EMPTY_TEXT i want at one command to clear all gui. right now i have only 2 text boxes and it's simple but in the future i have 50-60 text boxes and i looking for another way to clear the text and not to set for each textbox the text to empty.

Upvotes: 0

Views: 91

Answers (2)

Noam M
Noam M

Reputation: 3164

Assuming you're using MVVM and the next code is your UI:

<Grid>
    <TextBlock Text="Name:"/>
    <TextBox Text="{Binding NewName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
    <Button Command="{Binding ResetCommand}"/>
</Grid>

When MyClass is the DataContext

public class MyClass : INotifyPropertyChanged
{
    #region INotifyPropertyChanged

    // ....

    #endregion

    #region Data Members

    private ICommand _resetCommand;
    private string _newName;

    #endregion

    #region Data Members

    public ICommand ResetCommand
    {
        get
        {
            if (_restNewCCommand == null)
                _resCommand = new DelegateCommand(resetCommand, canResetCommand);

            return _resetCommand;
        }
    }

    public string NewName
    {
        get
        {
            return _newName;
        }
        set
        {
            _newName = value;
            OnPropertyChanged("NewName");
        }
    }

    #endregion

    #region Commands Callbacks

    private void resetCommand(object obj)
    {
       //Reset inputs
        NewName = String.Empty;
    }

    private bool canResetCommand(object obj)
    {
        return true;
    }

    #endregion
}

Note: The implementation of class DelegateCommand can be taken/deduced from here.

Upvotes: 2

BVer
BVer

Reputation: 41

Do you perhaps have your XAML and code behind ? If you want to empty the controls from code behind, you should give them a name to be able to access them of course, then you can just empty them as S.Akbari suggested.

Upvotes: 0

Related Questions