假设我有2个区域A和B.
地区A:
<Grid>
<TextBlock Name="tba"> HAHAHA </TextBlock>
</Grid>
B区:
<Grid>
<TextBlock Text="{Binding ElementName=tba, Path=Text}"/>
</Grid>
这不起作用。修复此问题的解决方法是什么,因此在区域B中还会显示“HAHAHA”?
答案 0 :(得分:2)
您的视图模型可以相互通信,以通过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
基本上是一回事(在这个简单的例子中)。