棱镜:如何绑定东西shell.xaml到一个区域的内容变量? [英] prism: how to bind something in shell.xaml to a region content variable?

查看:157
本文介绍了棱镜:如何绑定东西shell.xaml到一个区域的内容变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应用在棱镜/ MVVM / MEF和像StockTraderRI属性使用负载。

App is in prism/mvvm/mef and uses loading by attribute like StockTraderRI.

我的shell窗口包含了一个状态,这是全球性的壳,不是本地的每个区域一个DockPanel中

My shell window contains a DockPanel for a StatusBar, which is global to the shell, not local to each region.

看起来是这样的:

<DockPanel>
  <StatusBar DockPanel.Dock="Bottom">
    <StatusBarItem>
      <TextBlock Text="{Binding Source={x:Static bcl:Configuration.Global}, Path=LoggedOn.User}"/>
    </StatusBarItem>
    <StatusBarItem>
      <TextBlock Text="{Binding StateMessage}"/>
    </StatusBarItem>
  </StatusBar>
  <ContentControl x:Name="MainContent" cal:RegionManager.RegionName="MainRegion"/>
</DockPanel>

绑定到一个全局变量不工作。现在,我想绑定的StateMessage在StatusBarItem在任何控件加载到MainRegion财产StateMessage。结果
我的第一个猜测是使用类似:

Binding to a global variable does work well. Now I'd like to bind the StateMessage in the StatusBarItem to the property StateMessage in whatever control is loaded into the MainRegion.
My first guess was to use something like:

<TextBlock Text="{Binding Path=DataContext.StateMessage,Source={StaticResource MainContent}}"/>

但是,这当然是不行的,因为日程地址搜索Maincontent没有StaticResource的。

But this of course does not work, since MainContent is no StaticResource.

任何人都可以点我的Text属性加载到MainRegion的用户控件的一些属性绑定的方式?

Can anyone point me to a way to bind the Text property to some property of the UserControl loaded into the MainRegion?

推荐答案

我不认为你要能够找到一个干净的方式直接绑定到一个属性在一个区域中的对象。你也许可以做到这将是在指定区域的所有意见的一种方式,从一个通用的接口继承,这样,它们都实现了相同的属性(比如说的SystemName)。您可以绑定到包含该属性的路径,并使用ValueConverter从System.Object的类型更改为接口类型。

I don't think you're going to be able to find a clean way to bind directly to a property in an object in a region. One way you might be able to do this would be to have all Views in the specified region inherit from a common interface, such that they all implement the same Property (say, SystemName). You may be able to bind to a path that includes that property, and use a ValueConverter to change the type from System.Object to the interface type.

我有这样做的问题是,它限制了你可以把进入该地区,并似乎对夫妇在它显示超过我个人比较喜欢的类型外壳。我建议不同的路线:因为你的状态栏是你的shell的一部分,有一个视图模型与你的财产的外壳,状态栏结合,并使用事件跟踪状态值更改时。从这样几个好处:

The problem I have with that is that it limits what you can put into the region, and seems to couple the shell with the types displayed in it more than I'd personally like. I'd suggest a different route: since your status bar is part of your shell, have a ViewModel for the shell with your property that the status bar binds to, and that uses an event to track when the status value changes. A few benefits from doing this:


  1. 您的事件可以用任何数量的你的shell可以绑定到的属性发送任何对象。这让比你的状态栏上其他的东西也利用它。

  2. 不与在壳上的各种对象进行交互浏览不必发送该事件。这让该获得显示在区域与壳不会不利相互作用,或实现它们不需要
  3. 任何接口较大组分的子视图
  4. 它是在棱镜框架明确定义的机制,并且因此来分发信息的期望的方式

  1. Your event can send any object with any number of properties that your shell can bind to. This lets things other than your status bar also utilize it.
  2. Views that don't interact with the various objects in your shell don't have to transmit the event. This lets subviews of a larger component that get displayed in the region not adversely interact with the shell, or implement any interfaces that they don't need to
  3. It is a well defined mechanism in the Prism framework, and hence an expected way to distribute information

请注意,你可以明显地定义事件是你需要什么 - 我只是把Object类型的参数,但你可能会想,要更具体到你的总体需求

Note that you could obviously define the event to be whatever you need - I just put in a parameter of type Object, but you'd probably want that to be more specific to your overall needs.

// Use normal binding for Shell -> ShellViewModel
public sealed ShellViewModel : INotifyPropertyChanged
{
   private readonly IEventAggregator = eventAggregator;

   public ShellViewModel(IEventAggregator eventAggregator)
   {
      _eventAggregator = eventAggregator;

      _eventAggregator.GetEvent<MainRegionViewLoaded>.Subscribe(
         new Action<Object>(HandleMainRegionViewLoaded));
   }

   private String _statusName;
   public String StatusName
   {
      get { return _statusName; }
      set
      {
         if (_statusName != value)
         {
            _statusName = value;
            RaisePropertyChanged("StatusName"); // Not showing this...
         }
      }
   }

   private void HandleMainRegionViewLoaded(Object param)
   {
      // Not doing all the checking here, such as null, etc.
      var statusName = param as String;
      StatusName = statusName;
   }
}

现在,您的所有意见必须做的是落实IActiveAware,并引发该事件时,他们变得​​活跃。或者,你可以有视图模型对您的视图做到这一点(这可能是更好的,但需要一些额外的通讯有一些解释的这里上这样做)。

Now, all your views have to do is implement IActiveAware, and raise the event when they become Active. Alternatively, you could have the ViewModel's for your View do this (which is probably better, but would require some additional communication. There's some explanation here on doing that.).

public void MainView : UserControl, IActiveAware
{

   private readonly IEventAggregator _eventAggregator;

   public MainView(IEventAggregator eventAggregator)
   {
      InitializeComponent();

      _eventAggregator = eventAggregator;
   }

   private bool _isActive;
   public bool IsActive
   {
      get { return _isActive; }
      set
      {
         if (value)
         {
            _eventAggregator.GetEvent<MainRegionViewLoaded>.Publish(new MainRegionViewLoaded("My Name"));
         }
         _isActive = value;
      }
   }
}

这篇关于棱镜:如何绑定东西shell.xaml到一个区域的内容变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆