简体   繁体   中英

WPF refresh master detail page after save

I have a desktop application that allows for updating information in a table from multiple laptops. The program was written so if the network drops people can keep working. Because of that is has a dedicated save and refresh button instead of 2 way binding. But i cannot get the refresh to work. It is using a listbox as master and a grid for details.

XAML:

<Window.Resources>
    <CollectionViewSource x:Key="nJSSEntriesViewSource" d:DesignSource="{d:DesignInstance my:NJSSEntry, CreateList=True}" CollectionViewType="{x:Type ListCollectionView}"/>
</Window.Resources>
<Grid DataContext="{StaticResource nJSSEntriesViewSource}" Height="858">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="200"/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="720*" />
        <RowDefinition Height="138"/>
    </Grid.RowDefinitions>
    <ListBox Height="626" HorizontalAlignment="Left" Name="listBox1" VerticalAlignment="Top" Width="200" DisplayMemberPath="FullName" ItemsSource="{Binding}" Margin="0,25,0,0" />

C#

private CombinedShowsContext _combinedShowsContext;
private CollectionViewSource _nJssEntriesViewSource;

private ListCollectionView _showsEntriesView;

private System.Data.Objects.ObjectQuery<NJSSEntry> GetNJSSEntriesQuery(CombinedShowsContext combinedShowsContext)
{
    System.Data.Objects.ObjectQuery<NJSSEntry> nJssEntriesQuery = combinedShowsContext.NJSSEntries;
    // Returns an ObjectQuery.
    return nJssEntriesQuery;
}

private void Window_Loaded(object sender, RoutedEventArgs e)
{

    _combinedShowsContext = new CombinedShowsContext();

    _nJssEntriesViewSource = ((CollectionViewSource)(FindResource("nJSSEntriesViewSource")));
    System.Data.Objects.ObjectQuery<NJSSEntry> nJssEntriesQuery = GetNJSSEntriesQuery(_combinedShowsContext);
    _nJssEntriesViewSource.Source = nJssEntriesQuery.Execute(System.Data.Objects.MergeOption.AppendOnly);

    _showsEntriesView = (ListCollectionView) _nJssEntriesViewSource.View;

}

private void RefreshButton1_Click(object sender, RoutedEventArgs e)
{
    try
    {
        //_nJssEntriesViewSource.View.Refresh();
        //_showsEntriesView.Refresh();
        //listBox1.Items.Refresh();
        CollectionViewSource.GetDefaultView(listBox1.ItemsSource).Refresh();
        MessageBox.Show("Refreshed", "Refreshed",
                                                            MessageBoxButton.OK);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Refresh Failed",
                                                            MessageBoxButton.OK);
    }

}

I have been testing this on two VMS with the program installed. I will update and save on one and then hit refresh on the other and it should show the changes. Currently nothing happens.

I have to believe I am refreshing the wrong item or object.

I would eventually like to update the listbox items with colors to show if a entry was updated or not after the refresh.

If I understand you correctly, you are having trouble refreshing your views. If your views have never worked, or displayed any data, then this advice will be of no help to you. However, if your view does load data when it is loaded, then you just need to think a little bit differently about your problem.

You say that your view won't refresh, but (if you're still reading, then I'm going to assume this) your view will initially load data. These two things are not mutually exclusive... in fact refreshing a view can often be exactly the same as loading a view... you just have to reload the data in the view.

So, my advice to you is for you to look at your initial data loading code and just call that (or just the relevant parts of it) again when the user wants to refresh the UI.


UPDATE >>>

There is an alternative, but it's a lot more work. I've written some live dashboard applications before, so I think you might be after the same kind of thing. What I had to do after the initial load of data, was to continually query the database for newer data. When any arrived, rather than re-loading all of the collections, the new items were simply added to them.

However, the problem is that you also have to query the database for any data that has changed, so then you need to do loads of data comparisons to update the objects that have changed and remove the ones that have been deleted. It's a lot of work, but it gives you another option.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM