棱镜-跨区域数据绑定 [英] Prism - Cross Region DataBinding

查看:79
本文介绍了棱镜-跨区域数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有两个区域A和B。

Lets say i have 2 regions A and B.

区域A:

<Grid>
    <TextBlock Name="tba"> HAHAHA </TextBlock>
</Grid>

区域B:

<Grid>
    <TextBlock Text="{Binding ElementName=tba, Path=Text}"/>
</Grid>

这不起作用。解决此问题的解决方法是什么,因此在区域B中还会显示 HAHAHA?

This does not work. What is the workaround to fix this, so in region B also "HAHAHA" is displayed ?

推荐答案

您的视图模型可以与之通信彼此通过 EventAggregator 建立连接。

Your view models can communicate with each other to make the connection via EventAggregator.

// needs to be public if the two view models live in different assemblies
internal class ThePropertyChangedEvent : PubSubEvent<string>
{
}

internal class ViewAViewModel : BindableBase
{
    public ViewAViewModel( IEventAggregator eventAggregator )
    {
        _eventAggregator = eventAggregator;
        eventAggregator.GetEvent<ThePropertyChangedEvent>().Subscribe( x => TheProperty = x );
    }

    public string TheProperty
    {
        get { return _theProperty; }
        set
        {
            if (value == _theProperty)
                return;
            _theProperty = value;
            _eventAggregator.GetEvent<ThePropertyChangedEvent>().Publish( _theProperty );
            OnPropertyChanged();
        }
    }

    #region private
    private readonly IEventAggregator _eventAggregator;
    private string _theProperty;
    #endregion
}

... ViewBViewModel 本质上是同一件事(在此简单示例中)。

... ViewBViewModel is essentially the same thing (in this simple example).

这篇关于棱镜-跨区域数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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