Stacked
Stacked

Reputation: 7336

Wpf binding is not working properly

My ViewModel

public class MyViewModel : ViewModelBase
        {
            // INotifyPropertyChanged is implemented in ViewModelBase

            private String _propX;
            public String PropX
            {
                get { return _propX; }
                set
                {
                    if (_propX != value)
                    {
                        _propX = value;
                        RaisePropertyChanged(() => PropX);
                    }
                }
            }

            private String _propY;
            public String ServerIP
            {
                get { return _propY; }
                set
                {
                    if (_propY != value)
                    {
                        _propY = value;
                        RaisePropertyChanged(() => ServerIP);
                    }
                }
            }

            public A()
            {
                this._propY = "000.000.000.000";
                this._propY = "000.000.000.000";
            }
        }

// EDIT
// This is the command that resets the properties
    private RelayCommand _resetFormCommand;
    public ICommand ResetConnectionFormCommand
    {
        get
        {
            if (_resetFormCommand == null)
            {
                _resetFormCommand = new RelayCommand(param => this.ExecuteResetFormCommand(), param => this.CanExecuteResetFormCommand);
            }

            return _resetFormCommand;
        }
    }

    private bool CanExecuteResetFormCommand
    {
        get
        {
            return !String.IsNullOrWhiteSpace(this._propX) ||
                !String.IsNullOrWhiteSpace(this._propY);
        }
    }

    private void ExecuteResetFormCommand()
    {
        this._propX = "";
        this._propY = "";
    }

My View xaml

<TextBox Name="propX" Text="{Binding PropX }" PreviewTextInput="textBox_PreviewTextInput" />
<TextBox Name="propY" Text="{Binding PropY }" PreviewTextInput="textBox_PreviewTextInput" />
<Border>
    <Button Content="Reset" Name="resetBtn" Command="{Binding ResetFormCommand}" />
</Border>

My View code behind

private MyViewModel vm;
public ConnectionUserControl()
{
    InitializeComponent();
    vm = new MyViewModel();
    this.DataContext = vm;
}

private void textBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
    ValidateInput(sender as TextBox, e);
}

The reset command resets the properties in my view model but the textboxes still contain their values, the binding is not working properly :( Am i missing something here?

Upvotes: 1

Views: 553

Answers (5)

Bob.
Bob.

Reputation: 4002

In your Code-behind, you have a property ServerIP, which I think you wanted to be named as PropY, since your TextBox binds to a PropY property.

<TextBox Name="propY" Text="{Binding PropY }" PreviewTextInput="textBox_PreviewTextInput" /

Also, you should be assigning the value to your property in your ExecuteResetFormCommand command, and not your private member since the private member does not trigger INotifyPropertyChanged

private void ExecuteResetFormCommand()
{
    this.PropX = "";
    this.PropY = ""; // <-- Currently you have PropY declared as ServerIP
}

Upvotes: 0

user1064519
user1064519

Reputation: 2190

You should reset the properties, not the private members:

private void ExecuteResetFormCommand() { this.PropX = ""; this.PropY = ""; }

Upvotes: 3

Syed Waqas
Syed Waqas

Reputation: 862

binding has to be two way in order for textbox to update itself from viewmodel

Upvotes: 0

Tomtom
Tomtom

Reputation: 9394

In your xaml-code you have to set the binding like:

<TextBox Name="propX" Text="{Binding PropX, Mode=TwoWay}" .../>

Upvotes: 0

Paymahn Moghadasian
Paymahn Moghadasian

Reputation: 10349

How are you resetting the values? You may be overriding the databinding when you reset the values. It would be helpful if you post the code that gets executed when the button is clicked.

Upvotes: 0

Related Questions