How can we improve the Windows dev platform?

Changing DataContext modifies previously bound view model

Using the fall creator's update version of UWP, it seems that when a data context is changed to a new view model, the previous view model is updated with null values. This is the same as if the DataContext is set to null before changing view models. It doesn't make sense why the bound values are updated when the DataContext is changed.

3 votes
Sign in
Check!
(thinking…)
Reset
or sign in with
  • facebook
  • google
    Password icon
    Signed in as (Sign out)

    We’ll send you updates on this idea

    robloo shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

    9 comments

    Sign in
    Check!
    (thinking…)
    Reset
    or sign in with
    • facebook
    • google
      Password icon
      Signed in as (Sign out)
      Submitting...
      • robloo commented  ·   ·  Flag as inappropriate

        Yes, it appears this isn't an issue with the DataContext in general, it's related only to ComboBoxes that have a bound ItemsSource as well as SelectedItem. Create a new UWP project targeting on Fall Creators Update and put the following in the MainPage source files. It's quick and dirty but if you put a breakpoint on the button event and watch what happens when the second view model is set, you will see the selected item is cleared.

        XAML:
        <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <StackPanel>
        <ComboBox ItemsSource="{Binding Items}"
        SelectedItem="{Binding Selection, Mode=TwoWay}"/>
        <Button Content="Text"
        Click="Button_Click" />
        </StackPanel>
        </Grid>

        C#:
        public sealed partial class MainPage : Page
        {
        ViewModel vm1 = new ViewModel();
        ViewModel vm2 = new ViewModel();

        public MainPage()
        {
        this.InitializeComponent();

        vm1.Selection = "Value 3";
        vm2.Selection = "Value 2";

        this.DataContext = vm1;
        }

        private class ViewModel : INotifyPropertyChanged
        {
        /// <summary>
        /// Event that occurs when a property value changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        private string _Selection;
        private ObservableCollection<string> _Items = new ObservableCollection<string>();

        public ViewModel()
        {
        this._Items.Add("Value 1");
        this._Items.Add("Value 2");
        this._Items.Add("Value 3");
        }

        public string Selection
        {
        get { return (this._Selection); }
        set
        {
        this._Selection = value;
        this.NotifyPropertyChanged(nameof(this.Selection));
        }
        }

        public ObservableCollection<string> Items
        {
        get { return (this._Items); }
        }

        /// <summary>
        /// Raises the property changed event for the given property name.
        /// </summary>
        /// <param name="propertyName">The name of the property being changed.</param>
        private void NotifyPropertyChanged(string propertyName)
        {
        this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        return;
        }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
        this.DataContext = vm2;
        return;
        }
        }

      Feedback and Knowledge Base