我在窗户上挂了一台ListBox。此窗口的模型使用具有导体的Caliburn:
public class ShellViewModel : Conductor<IScreen>.Collection.OneActive我的屏幕是用户控件(每个UC都有一个型号),当我单击TabControl上的选项卡时会加载这些控件。
我希望能够在所有屏幕上访问ListBox selected item。
我该怎么做?
发布于 2014-07-28 00:18:42
我可以使用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);在我的main 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;
}更多信息:Caliburn Micro Part 4: The Event Aggregator
发布于 2014-07-31 20:44:35
另一种选择(虽然耦合程度更高)是利用屏幕有一个"Parent“属性这一事实,您可以使用该属性来访问它们的conductor;因此,您可以在MyScreenViewModel中执行类似以下操作。
void GetSelectedThing()
{
var conductingVM= this.Parent as ShellViewModel ;
this.SelectedThing = conductingVM.SelectedThing;
}https://stackoverflow.com/questions/24974468
复制相似问题