2

I am developing a desktop application using Modern UI for WPF. I try to refresh my tab page when I go to a new tab page, but I couldn't.

I want to refresh my MUI WPF tab page when I go to another page using my tab controller.

Can anyone help me?

har07
  • 88,338
  • 12
  • 84
  • 137
Sandun Harshana
  • 721
  • 1
  • 13
  • 28

3 Answers3

1

I'm not quite sure what you mean exactly, but by calling InvalidateVisual() on a control, you can force a visual refresh of it if that's what you're after, as it sounds like you've got a WPF control that isn't being updated when the data is changing.

Based on the MSDN documentation, this:

Invalidates the rendering of the element, and forces a complete new layout pass. OnRender is called after the layout cycle is completed.

For example:

        var grid = new Grid();
        // Our grid should refresh after this, 
        // although in normal circumstances it would by default regardless.
        grid.InvalidateVisual();    

I hope that this is of use.

TernaryTopiary
  • 436
  • 5
  • 17
  • 1
    When I go to tab page and do transaction like delete row,it is not refresh.I want to reload my tab page when I come to tab page from another one.thanks – Sandun Harshana May 06 '14 at 08:08
  • 1
    From the mention of transactions, it sounds like you're using some sort of database. In that case, you will want to perform the query on your database again and repopulate the UI elements accordingly. – TernaryTopiary May 07 '14 at 04:22
  • In one tab page I edit user details and update database.when i go to another tab page and come again to edited tab page, I cant see edit details.but database was updated.to see new details i have run the program again. – Sandun Harshana May 09 '14 at 04:43
  • 1
    If it was me, my first idea would be to check the query results to make sure your changes are going to the database, either while debugging or in a third party program designed to browse databases. If your database is being updated as you say it is, then to me it sounds like changes aren't being propagated from the database to your interface (aka: the queries you are using to get data to the interface aren't being run on the OnTabChanged event for the TabControl), so I would investigate that. You may need to re-run the query for the tab that you want to update. – TernaryTopiary May 09 '14 at 05:32
  • Exactly what I had to do - re-query the database and re-bind the results. `grid.InvalidateVisual();` did not do it automatically, for me. – vapcguy Jan 13 '17 at 20:49
0

You can use SelectionChanged event to handle this. You can refresh MUI WPF tab page by using SelectionChanged.

<TabControl x:Name="MyTab" SelectionChanged="MyTabControl_SelectionChanged">
    <TabItem x:Name="TabItem1" Header="Tab 1"/>
    <TabItem x:Name="TabItem2" Header="Tab 2"/>
    <TabItem x:Name="TabItem3" Header="Tab 3"/>
</TabControl>

Then you can access to each TabItem at the event:

private void MyTabControl_SelectionChanged(object sender, SelectionChangedEventArgs e){
    if (TabItem1.IsSelected){}
    if (TabItem2.IsSelected){}
    if (TabItem3.IsSelected){}  
}
manitaz
  • 1,181
  • 2
  • 9
  • 26
  • 1
    A good start, but what do you add inside the brackets?? I tried `TabItem1.InvalidateVisual()` (as suggested below) and `TabItem1.UpdateLayout()` (per the answer here - http://stackoverflow.com/questions/33974939/c-sharp-wpf-tabcontrol-tabitem-refresh ) and neither works when clicking my first tab. – vapcguy Jan 12 '17 at 21:07
  • Had a similar issue to the OP and what I ended up doing was something like TernaryTopiary said in his comments, below. You have to manually re-query and re-bind your ListViews/GridViews. So I just did this in the routine that was saving my data. When I tried doing it on `SelectionChanged` when `.IsSelected()`, as suggested above, it was trying to rebind at the same time, I suspect, as doing the initial loading/binding. It caused a StackOverflowException, for me. – vapcguy Jan 13 '17 at 20:46
0

While the selected answer is ok, it might not be the best way to go about it.

MUI includes a content navigation framework that handles content loading, unloading and history navigation based on link uris. If you want your content to be aware of navigation events such as loaded and unloaded events, you'll need to implement an interface.

Make your content navigation aware by implementing the IContent interface available in the FirstFloor.ModernUI.Windows namespace.

A simple Example is :

public class MyContent : UserControl, IContent
{
  public void OnFragmentNavigation(FragmentNavigationEventArgs e)
  {
  }
  public void OnNavigatedFrom(NavigationEventArgs e)
  {
  }
  public void OnNavigatedTo(NavigationEventArgs e)
  {
    //Refresh your page here
  }
  public void OnNavigatingFrom(NavigatingCancelEventArgs e)
  {
    // ask user if navigating away is ok
    if (ModernDialog.ShowMessage("Navigate away?", "navigate", MessageBoxButton.YesNo) == MessageBoxResult.No) {
      e.Cancel = true;
    }
  }
}