Prism - Cross Region DataBinding

喜欢而已 提交于 2019-12-18 09:32:45

问题


Lets say i have 2 regions A and B.

Region A:

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

Region B:

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

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


回答1:


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 is essentially the same thing (in this simple example).



来源:https://stackoverflow.com/questions/38767926/prism-cross-region-databinding

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!