开发者

WPF: Navigation/Design: How to keep track of state so I can change appearance of buttons?

开发者 https://www.devze.com 2023-01-10 11:28 出处:网络
I have an application that has a top level navigation menu which consists of series of buttons within a stackpanel. When a user clicks on a button the view model processes the command and updates the

I have an application that has a top level navigation menu which consists of series of buttons within a stackpanel. When a user clicks on a button the view model processes the command and updates the value of CurrentView (type UserControl). The CurrentView is bound to the element ContentControl as below.

<ContentControl Content="{Binding CurrentView}" />

I want the 'menu' to keep track of where the user is so that I can change the foreground of the navigation buttons, so users know where they are. What is the best way to do this? Should I wrap this 'menu' into a control?

Some of the views passed to the ContentControl will hav开发者_如何学编程e their own sub menus. These submenus work in the same way, and I would like to change the foreground and background for these.


Most of what you are talking about here is typically done by using a Frame, and Navigation with Pages of content. Is there a specific reason you are not using this?

For Example:

<sdk:Frame x:Name="CenterFrame" BorderThickness="0" Source="/Home">
<sdk:Frame.UriMapper>
    <sdk:UriMapper>
        <sdk:UriMapping Uri="/Job/{ID}" MappedUri="/Views/JobView.xaml?ID={ID}"/>
        <sdk:UriMapping Uri="/Home" MappedUri="/Views/HomeView.xaml"/>
        <sdk:UriMapping Uri="/Resource/{ResourceName}" MappedUri="/Views/ResourceView.xaml?Resource={ResourceName}"/>
        <sdk:UriMapping Uri="/Tasks" MappedUri="/Views/TaskView.xaml"/>
    </sdk:UriMapper>
</sdk:Frame.UriMapper>

And then within your page:

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        if (this.NavigationContext.QueryString.ContainsKey("Resource"))
        {
            ResourceViewModel rvm = ViewModelLocator.ResourceVMStatic;
            if (rvm != null)
                rvm.ResourceName = this.NavigationContext.QueryString["Resource"];
        }
        base.OnNavigatedTo(e);
    }

If you don't want to use pages, and want to use controls, you can do that, but you will have to provide the tracking manually. I would recommend using a view model just for your navigation, which can persist some data itself.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号