WPF MVVM navigate views

后端 未结 4 1132
面向向阳花
面向向阳花 2020-11-22 05:28

I have a WPF application with multiple views. I want to switch from view 1 to view 2 and from there I can switch to multiple views. So I want a button on view 1 that loads v

4条回答
  •  一整个雨季
    2020-11-22 05:57

    Maybe this link will help you. Just set the NavigateTo property to the view which you need to display on the window.

    As an example you can do something like

    
    
        

    Then the class file would be

    public partial class MainWindowView : Window
    {
        public MainWindowView()
        {           
                  InitializeComponent();
        }
    
            public ContentControl ViewContainer { get { return _viewContainer; } }
    
        }
    

    Then you can define each view as UserControl and then using the link I gave above bind the button's meffed:NavigationExtensions.NavigateTo="secondView". To target the ContentControl of the Window just use a RelativeSource binding. For e.g

    meffed:NavigationExtensions.NavigationHost="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}},Path=ViewContainer}"

    In each of the view just see that you annotate the code behind class definition with the [NavigationView("firstview")] and so on.

    It is complicated for first time but it will be very easy once you understand the idea.

提交回复
热议问题