A.B.
A.B.

Reputation: 2470

Prism - Cross Region DataBinding

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 ?

Upvotes: 1

Views: 216

Answers (1)

Haukinger
Haukinger

Reputation: 10883

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).

Upvotes: 2

Related Questions