4

In the examples for coding with the MVVM pattern and WPF binding, they use INotifyPropertyChanged when it's a single value and ObservableCollection when it is a list of values.

I've also read, you cannot use static variables with INotifyPropertyChanged, but you can with ObservableCollection. I'd like to bind to a static variable.

The easiest (to me at least) workaround, is to use an ObservableCollection and always just use and bind to index 0. Is this appropriate to do? Is there any benefit to using INotifyPropertyChanged instead of an ObservableCollection?

For example: This seems to be the simplest workaround:

public static ObservableCollection<int> MyValues { get; set;  }

<TextBox Text="{Binding MyValue[0]}">

For wanting to do this:

private static int _myValue;
public static int MyValue //does not work
{ 
    get { return _myValue; }
    set { _myValue = value; OnPropertyChange(); }
}

<TextBox Text="{Binding MyValue, UpdateSourceTrigger=PropertyChanged}">
dvdhns
  • 3,026
  • 2
  • 15
  • 11
  • "Is this appropriate to do?" If it works, then it works. As long as you don't encounter any performance issues, then I think it is fine. Because like you said, you can't use a static property with INotifyPropertyChanged. I don't know of any benefit of using INPC over an ObservableCollection, because they are usually used in different ways, but in this case, you can't use INPC on your static property. – Zack Apr 22 '16 at 20:09
  • You also might be interested in the accepted answer on this question http://stackoverflow.com/questions/14614190/inotifypropertychanged-and-static-properties – Zack Apr 22 '16 at 20:11
  • You can in theory do change notifications with a static property (`public static event EventHandler StaticPropertyChanged`, blah blah) but... I've tried it, it didn't work, and your hack looks like a lot less trouble and would actually work. Thanks! – 15ee8f99-57ff-4f92-890c-b56153 Apr 22 '16 at 20:24
  • 1
    Wrap the property in a view model, put the view model in a static property. Done. –  Apr 22 '16 at 20:50
  • 1
    I would better move that static property to another class, make singleton out of it, and then bind as usual. At least looks not worse that using collection with single element for that. – Evk Apr 22 '16 at 21:08
  • 2
    You can also define regular (non-static) property in your view model, but make it reference static field in your getter\setter. This way you still do regular bindings but achieve the same effect as with static property. – Evk Apr 22 '16 at 21:14

2 Answers2

2

I don't think your analogy is fair(Comparing ObservableCollection with directly with Primitive Type property). Compare ObservableCollection to below class

public class MyClass : INotifyPropertyChanged
{

    private string text;

    public string Text
    {
        get { return text; }
        set { text = value;
        RaiseChange("Text");
        }
    }


    public event PropertyChangedEventHandler PropertyChanged;
    private void RaiseChange(string prop)
    {
        if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(prop)); }
    }
}

Now both below will behave same:

 public static MyClass Text { get; set; }

 public static ObservableCollection<string> Text { get; set; }

If you perform Text.Text = "Hello" for MyClass or Text[0] = "Hello" for ObservableCollection, both will reflected in same way.

Now if you have to use a static property for binding then instead of ObservableCollection I'll advice you to write your new class cause ObservableCollection has many internal implementations which probably is of no use to you any actually consuming memory & perforamnce.

Kylo Ren
  • 8,551
  • 6
  • 41
  • 66
1

This link may help, it shows the difference between List, ObservableCollection and INotifyPropertyChanged.

Hope this help

Abdulsalam Elsharif
  • 4,773
  • 7
  • 32
  • 66