locked
Data binding to object fields in 8.1 universal app

    Question

  • I'm building a universal app for windows 8.1 and windows phone 8.1 and I'm somewhat puzzled with data binding. Many practices easily found on manual pages and all over the forums don't seem to be applicable to a universal app (like MultiBinding, ValidationRules, RelativeSource other then Self or TemplatedParent, etc).

    I'm trying to make a flexible system, so I don't have to declare all of the binding properties with a { get { ... } set { ... }} on a binding object for every component I have. This would yield a lot of boilerplate code, because I would have to dispatch PropertyChanged event from each setter.

    Here's what I've come up with by now:

    • I have a Page with
      x:Class="SomeClass"
      DataContext="{Binding RelativeSource={RelativeSource Self}}"
    • There's an object in SomeClass:
      public ViewModel Bindings { get; set; }
    • This object implements INotifyPropertyChanged and declares
      public string this[string name] {
          get { ... }
          set { /* do smth and dispatch PropertyChanged */ }
      }
    • I bind TextBox.Text like this:
      Text="{Binding Bindings[Property], Mode=TwoWay}"
    • I create an instance of Bindings in Page constructor and initialize Bindings["Property"]

    When binding is initialized, TextBox.Text gets initial value of Bindings["Property"], which is fine. When I type in the TextBox, Bindings[Property] gets updated, which is also correct.

    But when I assign Bindings["Property"] = "something" in page code, TextBox doesn't get updated. I guess, it's because the component is listening to DataContext.PropertyChanged, and it's DataContext.Bindings, who dispatches property updates.

    So, am I doing something wrong here?
    Are there better ways of achieving what I want?

    Thanks!




    Monday, November 24, 2014 12:14 PM

Answers

  • I think most of the examples you have seen are for Silverlight and/or WPF that have some different XAML construct than that of Universal Apps.

    The issue you have mentioned is because collection that you are using does not implement INotifyCollectionChanged. Typically one would use System.Collections.ObjectModel.ObservableCollection<T> to do what you need. But that collection is not a dictionary (key, value) thing. If you need to use something the way you have explained then I will suggest implementing one like this. It is WPF but should give you enough idea.


    -- Vishal Kaushik --

    Please 'Mark as Answer' if my post answers your question and 'Vote as Helpful' if it helps you. Happy Coding!!!

    Monday, November 24, 2014 6:45 PM

All replies

  • I think most of the examples you have seen are for Silverlight and/or WPF that have some different XAML construct than that of Universal Apps.

    The issue you have mentioned is because collection that you are using does not implement INotifyCollectionChanged. Typically one would use System.Collections.ObjectModel.ObservableCollection<T> to do what you need. But that collection is not a dictionary (key, value) thing. If you need to use something the way you have explained then I will suggest implementing one like this. It is WPF but should give you enough idea.


    -- Vishal Kaushik --

    Please 'Mark as Answer' if my post answers your question and 'Vote as Helpful' if it helps you. Happy Coding!!!

    Monday, November 24, 2014 6:45 PM
  • It is WPF but should give you enough idea.

    Thanks Vishal, I didn't realize I could bind to KeyValuePair collection of a dictionary. Sounds interesting since converters are available in universal apps and may be this is the way to go.

    Still I wonder if it's the usual practice to have a unique view model class implementing INotifyPropertyChanged for each and every interface with all of the properties in it. May be one is not expected to avoid that?

    Tuesday, November 25, 2014 10:00 AM