Search code examples
c#xamluwpwin-universal-app

UWP static binding failed


I have a DataGrid with its ItemsSource bound to a static variable of type ObservableCollecion<Music> like this:

        ItemsSource="{Binding AllSongs, Mode=OneWay}"

This variable, called AllSongs, is declared in the same page as the DataGrid.

However, DataGrid is not showing the Data in the AllSongs. I have manually set this MusicLibraryDataGrid.ItemsSource = AllSongs; in the constructor. Why is this happening?

Another question is that, after I set it manually, Why modifications on the AllSongs like sorting do not reflect on the DataGrid? Meaning that the DataGrid.ItemsSource is not in sync with the AllSongs. Is it possible to make them in sync?

A possible solution for my second question might be registering a CollectionChanged event of AllSongs to do MusicLibraryDataGrid.ItemsSource = AllSongs; in it. Will this rebinding harm the performance?


Solution

  • As far as I know binding on static property is not allowed. What I would do is create a property on the class that reads from the static one.

    It's been discussed here: https://social.msdn.microsoft.com/Forums/windowsapps/en-US/e0e426ba-4feb-4571-b80b-7ba8818079b6/uwpstatic-property-binding?forum=wpdevelop