0

I have the following textbox

<TextBox Grid.Column="1" 
Grid.Row="1" 
Name="groupAddressBox"
Width ="80" 
Text="{Binding Path=GroupAddress,  Converter={StaticResource groupAddressConverter}}"/>

When I change the text manually, it's all good.

But when I try to do this via a button

private void Test_Click(object sender, RoutedEventArgs e)
{
    groupAddressBox.Text = "0/0/1";
}

Although the text changes, the source is not updated, and when I click on ok, it recognizes the value that was there before the change. I cannot upgrade the source straight away, so I prefer to do this this way.

Is there something that can help me force the source upgrade via this way?

9
  • 3
    Is this Web, Winforms, WPF? Commented Mar 20, 2018 at 20:10
  • 2
    sorry, it's WPF Commented Mar 20, 2018 at 20:15
  • 4
    As soon as you assign a value using the Text property, your binding will not work any longer. You need to change the GroupAddress property instead and make sure that you use a two-way binding. Commented Mar 20, 2018 at 20:16
  • 1
    FYI: If you use MVVM, it is often not good to use click handlers in your controls. In the future, when you have warmed up some more with WPF/MVVM, let your ViewModel provide a command (relay command) which will be bound to the button. The command implementation (within the VM which is owning the command) would then for example set the GroupAddress property of the VM... Commented Mar 20, 2018 at 20:25
  • 1
    Thanks, I did exactly that, I added a command that could change the data source, and it works. Commented Mar 20, 2018 at 20:54

1 Answer 1

2

Based on your question, I tried to create a Simple Example of MVVM Pattern with very basic functionality. Please do necessary change to XAML and CS file as I took the highlighted code only.

Helper Classes

public abstract class ViewModelBase : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
            }
        }
    }



public class CommandHandler : ICommand
    {
        public event EventHandler CanExecuteChanged { add { } remove { } }

        private Action<object> action;
        private bool canExecute;

        public CommandHandler(Action<object> action, bool canExecute)
        {
            this.action = action;
            this.canExecute = canExecute;
        }

        public bool CanExecute(object parameter)
        {
            return canExecute;
        }

        public void Execute(object parameter)
        {
            action(parameter);
        }
    }

ViewModel

public class ViewModel : ViewModelBase
{ 
    private string groupAddress;
    public string GroupAddress
    {
        get
        {
            return groupAddress;
        }

        set
        {
            if(value != groupAddress)
            {
                groupAddress = value;
                OnPropertyChanged("GroupAddress");

            }
        }
    }

    public ViewModel() 
    { 

    } 

    private ICommand clickCommand; 
    public ICommand ClickCommand 
    { 
        get 
        { 
            return clickCommand ?? (clickCommand = new CommandHandler(() => MyAction(), true)); 
        } 
    } 

    public void MyAction() 
    { 
        GroupAddress = "New Group Address"; 
    } 
}

Window Xaml

<TextBox Grid.Column="1" Grid.Row="1" Width ="80" 
        Text="{Binding GroupAddress, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

<Button Content="Push" Style="{StaticResource TransparentButtonStyle}"
             Margin="5" Command="{Binding ClickCommand}"/>

Window Xaml cs

ViewModel vm = new ViewModel();

this.DataContext = vm;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.