我目前正在使用 MEFedMVVM 框架来访问 ViewModel,并想知道如何从当前正在使用的另一个 ViewModel 获取数据。这与 Cinch 的使用相结合。
目前我的标签控件定义如下:
<Window.Resources>
<DataTemplate DataType="{x:Type CinchV2:WorkspaceData}">
<AdornerDecorator>
<Border HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
CinchV2:NavProps.ViewCreator="{Binding}"/>
</AdornerDecorator>
</DataTemplate>
</Window.Resources>
加载视图后,我的主窗口 ViewModel 将按以下方式设置:
private void ViewAwareStatusService_ViewLoaded()
{
if (Designer.IsInDesignMode)
return;
//String imagePath = ConfigurationManager.AppSettings["YourImagePath"].ToString();
WorkspaceData loginWorkSpace = new WorkspaceData(null, "LoginUserControl", null, "Login", true);
WorkspaceData aboutWorkspace = new WorkspaceData(null, "About", null, "About", true);
WorkspaceData viewAlbumsWorkspace = new WorkspaceData(null, "ViewAlbums", null, "View Albums", true);
WorkspaceData readReviewSelectWorkspace = new WorkspaceData(null, "ReadReviewsSelect", null, "Select Review", true);
WorkspaceData adminWorkspace = new WorkspaceData(null, "Admin", null, "Admin", true);
Views.Add(aboutWorkspace);
Views.Add(loginWorkSpace);
Views.Add(readReviewSelectWorkspace);
Views.Add(viewAlbumsWorkspace);
SetActiveWorkspace(aboutWorkspace);
UserName = new DataWrapper<string>(this, UserNameChangeArgs);
UserName.IsEditable = true;
//UserName.DataValue = ConfigurationManager.AppSettings["UserName"];
UserRole = new DataWrapper<string>(this, UserNameChangeArgs);
UserRole.IsEditable = true;
//UserRole.DataValue = ConfigurationManager.AppSettings["UserType"];
}
MainWindow ViewModel 继承 ViewModelBase 类,其配置如下(Cinch 类):
namespace Cinch
{
public abstract class ViewModelBase : INotifyPropertyChanged, ICinchDisposable, IParentablePropertyExposer
{
public ViewModelBase();
public SimpleCommand<object, object> CloseActivePopUpCommand { get; }
public SimpleCommand<object, object> CloseWorkSpaceCommand { get; }
public string DisplayName { get; set; }
public bool IsCloseable { get; set; }
protected virtual bool ThrowOnInvalidPropertyName { get; }
public ObservableCollection<WorkspaceData> Views { get; set; }
public event EventHandler<EventArgs> ActivateRequest;
public event EventHandler<CloseRequestEventArgs> CloseRequest;
public event EventHandler<EventArgs> CloseWorkSpace;
public event Action<string> FocusRequested;
public event PropertyChangedEventHandler PropertyChanged;
public void Dispose();
public Delegate[] GetINPCSubscribers();
protected void NotifyPropertyChanged(PropertyChangedEventArgs args);
protected void NotifyPropertyChanged(string propertyName);
protected virtual void OnDispose();
public virtual void RaiseActivateRequest();
public virtual void RaiseCloseRequest(bool? dialogResult);
public void RaiseFocusEvent(string focusProperty);
public void SetActiveWorkspace(WorkspaceData viewnav);
[DebuggerStepThrough]
[Conditional("DEBUG")]
public void VerifyPropertyName(string propertyName);
}
现在我想要做的是,从 loginWorkspace 中的一个按钮单击,它是一个带有 ViewModel 的 UserControl,将 Admin 选项卡从 LoginViewModel 添加到 MainWindow 的 Views ObservableCollection。