开发者

DataGrid won't display data after refresh if ObservableCollection is null at startup

Hopefully I can make this clear. I have a DataGrid

<DataGrid Grid.Row="6" Grid.Column="1"
            AutoGenerateColumns="False"
            CanUserAddRows="False"
            CanUserDeleteRows="False"
            ItemsSource="{Binding projectEntriesForEmployee}">

bound to an

    public ObservableCollection<ProjectEntry> projectEntriesForEmployee { 
        get { return (ObservableCollection<ProjectEntry>)GetValue(projectEntriesForEmployeeProperty); }
        set { SetValue(projectEntriesForEmployeeProperty, value); }
    }
    public DependencyProperty projectEntriesForEmployeeProperty = Depende开发者_如何学PythonncyProperty.Register("projectEntriesForEmployee", typeof(ObservableCollection<ProjectEntry>), typeof(MainWindowVC));

If I set projectEntriesForEmployee before I load my UserControl (which I did to debug), my rows show properly in the DataGrid. If however, projectEntriesForEmployee is null when the UserControl loads, when I set projectEntriesForEmployee to a valid ObservableCollection with items in the list (based on an event), no rows show on the DataGrid. What could be going on?

Edit: I've tried

CollectionViewSource.GetDefaultView(projectEntriesForEmployee).Refresh();

but no joy.


I'm not sure what's going on, but I would try using Snoop to drill into the DataGrid and make sure that, after you update projectEntriesForEmployeeProperty the DataContext of the DataGrid is still set to the appropriate object and to verify the binding on ItemsSource. If there are any binding errors, Snoop will show them and it'll also let you drill into the object and see the full binding expression. Just a suggestion.


When your control loads, both your OC, and your DataGrid's ItemsSource both point to the same thing: a null piece of memory.

Some time later, your initialize your OC to a collection. Now your OC holds a collection, but your DataGrid's ItemSource still points to the same null piece of memory.

Can you not just initialize your OC in your control's constructor?

EDIT

I'm not a WPF guru, so there might be a reason to do this, but why are you setting your ObservableCollection as a dependency property? If all your doing is binding it to your DataGrid's ItemsSource, a regular, vanilla C# property will work fine, and still provide you all of the automatic updates that occur when you add to, or remove from the collection.


Sorry everyone. I've answered my own question, as it turns out that the problem was the Dependency Property Owner Type. I had it as MainWindowVC. It should have been TimeEntryVC. As soon as I changed that, I commented out the resetting the ItemsSource in the Controller class, and everything worked as it should. So @Tim, you're right that the Binding system takes care of things; I just told it to look for the property in the wrong class. Thanks all regardless, as I understand better what's going on behind the scenes now than I did before.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜