我在窗口上有一个ListBox
。这个Window的模型使用带有导体的Caliburn:
public class ShellViewModel : Conductor<IScreen>.Collection.OneActive
当我点击TabControl
上的标签时,我的屏幕是用户控件(每个UC都有一个模型)。
我希望能够在我的所有屏幕上访问ListBox
所选项目。
我该怎么做?
答案 0 :(得分:0)
我可以使用EventAggreagator
在我的AppBootstraper
课程中,我可以添加:
private CompositionContainer _container;
在Config
方法下:
var batch = new CompositionBatch();
batch.AddExportedValue<IWindowManager>(new WindowManager());
batch.AddExportedValue<IEventAggregator>(new EventAggregator());
batch.AddExportedValue(_container);
_container.Compose(batch);
在我的主ViewModel构造函数中:
IEventAggregator eventAggregator
在绑定属性中:
public Thing SelectedThing
{
get { return _selectedThing; }
set
{
_selectedThing = value;
NotifyOfPropertyChange(() => SelectedThing);
_eventAggregator.PublishOnUIThread(SelectedThing);
}
}
然后在我的屏幕模型上:
public class MyScreenViewModel : Screen, IHandle<Thing>
在构造函数中:
IEventAggregator eventAggregator
然后:
_eventAggregator = eventAggregator;
_eventAggregator.Subscribe(this);
接口实现:
void IHandle<Thing>.Handle(Thing selectedThing)
{
this.SelectedThing = selectedThing;
}
答案 1 :(得分:0)
另一种选择(虽然更加耦合)是使用屏幕有一个&#34;父母&#34;您可以用来接触他们的指挥的财产;所以你可以在MyScreenViewModel中执行以下操作。
void GetSelectedThing()
{
var conductingVM= this.Parent as ShellViewModel ;
this.SelectedThing = conductingVM.SelectedThing;
}