ListView data will not be updated in WPF application

I am trying to load data into a ListView in WPF using a stored procedure and Entity Framework structure. When I first load User Control, ListView loads the data just fine. Then I call the same code to update the data, and I can see through debugging that the ListItems counter is changing, but the data on the front screen is not updating.

Xaml

<ListView Name="DocsListView" ItemsSource="{Binding}">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Documents" DisplayMemberBinding="{Binding Documents}"/>
        </GridView>
    </ListView.View>
</ListView>

Code for

public void LoadDocs()
{
    Context _Context = new Context();
    DocsListView.ItemsSource = null;
    DocsListView.ItemsSource = _Context.SP_GetDocuments(1).ToList();
    _Context = null;
}

Can someone help me figure out what I'm doing wrong? I am using VS2012, .Net 4.5 and EF 5.0.

+4
source share
1 answer

I'm not sure what causes the problem, usually I would recommend using DependencyProperty!

ListView :

ICollectionView view = CollectionViewSource.GetDefaultView(DocsListView.ItemsSource);
view.Refresh();

, MVVM DesignPattern!

WPF MVVM . , , MVVMLight.

INotifyPropertyChanged.

XAML- ( DataContext)

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        DataContext="{Binding RelativeSource={RelativeSource Self}}">
    <Grid>
        <ListView Name="DocsListView" ItemsSource="{Binding Data}">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Documents" DisplayMemberBinding="{Binding Documents}"/>
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
</Window>

# Code-Behind (ObservableCollection , ):

public ObservableCollection<YourEntity> Data
{
     get { return (ObservableCollection<YourEntity>)GetValue(DataProperty); }
     set { SetValue(DataProperty, value); }
}

// Using a DependencyProperty as the backing store for Data.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty DataProperty =
DependencyProperty.Register("Data", typeof(ObservableCollection<YourEntity>), typeof(MainWindow), null);

public void LoadDocs()
{
     Context _Context = new Context();
     if(Data == null)
     {
         Data = new ObservableCollection<YourEntity>();
     }
     else
     {
         Data.Clear();
     }
     foreach(var doc in _Context.SP_GetDocuments(1).ToList())
     {
         Data.Add(doc);
     }
     _Context = null;
}
+2

All Articles