本文介绍了Reg:在WPF App中基于棱镜查看导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个用于学习目的的小应用程序.在我的外壳中,我使用了一个itemControl,它定义了一个区域,并且我已经向该区域注册了三个视图,在我的外壳中,我有另一个区域,并为其注册了一个视图,该视图包含3个按钮,我的要求是,当我单击button1,view1突出显示,其他2个视图被取消显示或禁用,以同样的方式单击第二个按钮时,view2应该被点亮.
我试图从4天开始实现这一目标,但是没有运气.

I created a small app for learning purpose . in my shell I took a itemsControl which defines a region and I have registered three views to this region,in my shell I have one another region and registed a view to it,the view contains 3 buttons,my requirement is that when I click on button1,view1 get highlighted and other 2 view either disapper or disable,same way when click second button,view2 should be liglllited.
I am trying to achive this since 4 days but no luck.
any help would be appericiated.

推荐答案

// Import the Microsoft.Practices.Prism.Regions, Microsoft.Practices.ServiceLocation and (maybe) Microsoft.Practices.Unity

IRegionManager myRegionManager = ServiceLocator.Current.GetInstance<IRegionManager>();



第二种方法可以在不使用依赖系统的情况下获得对RegionManager的引用.这使用RegionBehaviour的常规实现.这在 http://stackoverflow.com/questions/6582612/prism-4-locally- scoped-regionmanager [ ^ ].使用此答案中的代码,您可以使View和ViewModel知道它的RegionManager.


拥有regionmanager之后,您将需要对包含3个视图的区域的引用.



The second method makes it possible to get a reference to the RegionManager without using a Dependency System. This uses a cusom implementation of a RegionBehaviour. This is descibed in http://stackoverflow.com/questions/6582612/prism-4-locally-scoped-regionmanager[^]. Using the code in this answer you can make View and ViewModel aware of it''s RegionManager.


Once you have the regionmanager you will need a reference to the region containing the 3 views.

IRegion myRegion = myRegionManager.Regions["theNameOfYourRegion"];



要获取对视图的引用,请使用



To get a reference to the view, you use

//use namespace System.Linq

FrameworkElement aView = myRegion.Views.ElementAt(i) as FrameworkElement;




将所有内容放在一起,并假设您在后面使用代码来处理按钮的clickevent,则可能会得到以下内容:




Putting it all together and assuming that you use code behind to handle the clickevent of the buttons, you might get something like this:

private void Button1_Click(object sender, RoutedEventArgs e)
{
    ShowViewAtIndex(0);
}

private void Button2_Click(object sender, RoutedEventArgs e)
{
    ShowViewAtIndex(1);
}

private void Button3_Click(object sender, RoutedEventArgs e)
{
        ShowViewAtIndex(2);
}

private void ShowViewAtIndex(int index)
{
    IRegionManager myRegionManager = ServiceLocator.Current.GetInstance<IRegionManager>();
    IRegion myRegion = myRegionManager.Regions["theNameOfYourRegion"];

    int numberOfViews = myRegion.Views.Count();
    for (int i = 0; i < numberOfViews; i++)
    {
        FrameworkElement aView = myRegion.Views.ElementAt(i) as FrameworkElement;
        if (aView != null && i == index)
            aView.Visibility = System.Windows.Visibility.Visible;
        else
            aView.Visibility = System.Windows.Visibility.Collapsed;
    }
}



我希望这会有用.



I hope this is of any use.


这篇关于Reg:在WPF App中基于棱镜查看导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 04:43