Refresh/Reload MUI WPF Page when Tab is Changed

人盡茶涼 提交于 2019-12-06 09:17:02

问题


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?


回答1:


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){}  
}



回答2:


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.




回答3:


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;
    }
  }
}


来源:https://stackoverflow.com/questions/22877919/refresh-reload-mui-wpf-page-when-tab-is-changed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!