locked
Using Dependency properties as a binding -Source- RRS feed

  • Question

  • Hello

    i'm working on a windows store app for win8.1 rtm, but i've come across a very strage issue (at least for me, coming from wpf)

    i have a viewmodel with a dependency property, i bind it to a textblock, but when i update the value in the viewmodel, the textbox doesnt update. if i set a value on the view model before assigning it to the data context, that value shows up, so the actual binding seems fine.

    Are change notifications for dependency properties not supported in windows store apps? is there no other option than using INotifyPropertyChanged? it seems so strange, and such a hugely breaking change..

    viewmodel:

        public class ViewModel : DependencyObject
        {
            public static readonly DependencyProperty NameProperty = DependencyProperty.Register("Name", typeof(string), typeof(ViewModel), new PropertyMetadata(""));
            public string Name { get { return (string)GetValue(NameProperty); } set { SetValue(NameProperty, value); } }
        }

    view constructor:

            public MainPage()
            {
                this.InitializeComponent();
                DataContext = new ViewModel { Name ="hello" };
            }

    xaml

            <Button Click="Button_Click" Margin="0,11">buttom</Button>
            <TextBlock Grid.Column="1" Text="{Binding Name, Mode=TwoWay}"></TextBlock>

    Button click

    (DataContext as ViewModel).Name = "world";

    Tuesday, October 29, 2013 8:40 AM

Answers

  • Hi, i'm not sure why you have dependency properties in your view model...

    You should use accessors and INotifyPropertyChanged :

    public class MyViewModel : BindableBase
    {
     private string _myProperty;
            public string MyProperty
            {
                get { return _myProperty; }
                set
                {
                    this.SetProperty(ref _myProperty, value);
                }
            }
    }

    BindableBase is a class in the common folder when u create a new project, implementing INotifyPropertyChanged (so you can use the SetProperty() method).

    This way, you can bind your property and it will update when you change it.

     Edit : Well i missed the last paragraph of your post :P. I don't think u can use dependency properties in your viewmodel for what you want to achieve. Dependency properties are rather used in pages / controls, because you can bind a property to this dependency property. In winrt you need your control to have a dependency property and your viewmodel 's property to implement INotifyPropertyChanged to use binding.
    • Edited by Thomas Bovy Tuesday, October 29, 2013 9:29 AM
    • Marked as answer by aL3891 Tuesday, October 29, 2013 5:43 PM
    Tuesday, October 29, 2013 9:00 AM

All replies

  • Hi, i'm not sure why you have dependency properties in your view model...

    You should use accessors and INotifyPropertyChanged :

    public class MyViewModel : BindableBase
    {
     private string _myProperty;
            public string MyProperty
            {
                get { return _myProperty; }
                set
                {
                    this.SetProperty(ref _myProperty, value);
                }
            }
    }

    BindableBase is a class in the common folder when u create a new project, implementing INotifyPropertyChanged (so you can use the SetProperty() method).

    This way, you can bind your property and it will update when you change it.

     Edit : Well i missed the last paragraph of your post :P. I don't think u can use dependency properties in your viewmodel for what you want to achieve. Dependency properties are rather used in pages / controls, because you can bind a property to this dependency property. In winrt you need your control to have a dependency property and your viewmodel 's property to implement INotifyPropertyChanged to use binding.
    • Edited by Thomas Bovy Tuesday, October 29, 2013 9:29 AM
    • Marked as answer by aL3891 Tuesday, October 29, 2013 5:43 PM
    Tuesday, October 29, 2013 9:00 AM
  • I use dependency properties in wpf because they are more performant than INotifyPropertyChanged (because it uses reflection) and its a tiny bit less boilerplate, especially with some helper methods i have

    I wasnt aware of the BindableBase class, i've been using the blank template mostly, i'll check that out :)

    Does anyone know if its by design that custom dependency properties can't fire change notifications? i havent found any docs or articles about any of it.. i suppose i'll use INotifyPropertyChanged now though

    Tuesday, October 29, 2013 1:03 PM