0

Given the following classes (where ViewModelBase contains the INotifyPropertyChanged members)

namespace WpfApplication1
{
    class ViewModel : ViewModelBase
    {
        private Person selectedPerson;

        public ObservableCollection<Person> Persons { get; set; }

        public Person SelectedPerson
        {
            get
            {
                return selectedPerson;
            }
            set
            {
                selectedPerson = value;
                OnPropertyChanged("SelectedPerson");
            }
        }

        public ViewModel()
        {
            Persons = new ObservableCollection<Person>();
            Persons.Add(new Person { FirstName = "John", LastName = "Smith" });
            Persons.Add(new Person { FirstName = "Jane", LastName = "Jones" });

            SelectedPerson = new Person();
        }
    }

    class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
}

And the following XAML

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:vm="clr-namespace:WpfApplication1"
    Title="MainWindow" Height="350" Width="525">
  <Window.DataContext>
    <vm:ViewModel />
  </Window.DataContext>
  <Grid>
    <StackPanel>
      <ListBox ItemsSource="{Binding Persons}"
               DisplayMemberPath="FirstName"
               SelectedItem="{Binding SelectedPerson}"/>
      <StackPanel DataContext="{Binding SelectedPerson}">
        <TextBox Text="{Binding FirstName}" />
        <TextBox Text="{Binding LastName}" />
      </StackPanel>
    </StackPanel>
  </Grid>
</Window>

If the value in the TextBox bound to FirstName is changed, the change is automatically reflected in the ListBox. Assuming that I add a Button control and associated Command, is there a way instead to have the changed value propagated to the ListBox only if that Button has been pressed?

  • 1
    Try using `UpdateSourceTrigger=Explicit`. This link can help [http://www.wpf-tutorial.com/data-binding/the-update-source-trigger-property/](http://www.wpf-tutorial.com/data-binding/the-update-source-trigger-property/) – dev1998 Sep 18 '15 at 02:24
  • @dev1998: I assume you mean use UpdateSourceTrigger=Explicit on the TextBox. I prefer to use an ICommand for the Button (instead of coding a click handler in code-behind), so I'd need a way to get the value from the TextBox. Is there a way do do that other than, say, give the TextBox a name, and then in my command code get the Text value with something like((MainWindow)Application.Current.MainWindow).txtFirstName.Text? – OhGreatOne Sep 18 '15 at 13:57
  • This looks like it might be the MVVM solution you are looking for: [http://stackoverflow.com/questions/7657996/set-updatesourcetrigger-to-explicit-in-showdialog-wpf-mvvm](http://stackoverflow.com/questions/7657996/set-updatesourcetrigger-to-explicit-in-showdialog-wpf-mvvm) – dev1998 Sep 18 '15 at 18:26
  • @dev1998: I'll have to take a deeper look, but at first blush that solution appears to work for a single TextBox only. I realize that in my original post I mentioned on the First Name textbox, but that was for the sake of simplicity. In fact, though, using my original code, if either the first name or last name text box is updated, the change is persisted. Since only one ElementName can be specified in the CommandParameter, I could address only one of the two TextBoxes. I haven't yet read the MultiBinding part of the link you posted, so that might be the solution I need. (Maybe this weekend!) – OhGreatOne Sep 18 '15 at 18:38
  • It looks like the MultiBinding approach will work. My IMultiValueConverter Convert method returns the first parameter object array as a list of TextBox objects. That is then the argument to my ICommand method, where I loop through each TextBox object, get the binding expression in each, then call the UpdateSource method for the binding expression. – OhGreatOne Sep 19 '15 at 18:02

0 Answers0