[英]Update the Details Pane with the New User Control Window MVVM WPF
我將盡力詳細解釋該問題。
我正在使用WPF,MVVM和PRISM。 這是應用程序的結構:
CONTAINER
User control containing a toolbar
---------- LEFT PANE USER CONTROL
right pane
user control
----------
---------------------------------------------------------------
容器是承載應用程序外觀的Shell。 命令行管理程序還包含許多不同的用戶控件,例如用戶用戶控件,角色用戶控件,類別用戶控件。 所有控件均已折疊。
工具欄上有保存,編輯,刪除等選項。 當用戶從工具欄中選擇Edit選項時,我將使用WPF命令在ViewModel上調用Edit方法,如下所示:
private void Edit(UserViewModel userViewModel)
{
// load the edit page
}
工具欄.Datacontext動態設置為用戶控件的數據上下文,該控件是一個視圖模型。 因此,同一工具欄可以與不同的上下文一起使用,以在不同的視圖模型上調用命令。
至此,我已經獲得了在Edit方法中傳遞的正確的UserviewModel。 一切都很好! 但現在! 我需要更新詳細信息窗格以反映EditView。 我怎樣才能做到這一點? 所有控件都嵌入在折疊的Shell.xaml文件中。 此外,ViewModels在單獨的程序集中,而Shell在單獨的程序集中。
這是我需要幫助的地方! 由於不了解Shell的ViewModel怎么能(因為Shell引用ViewModel程序集而不是其他方法)對Shell不了解,因此可以將EditView注入到Shell詳細信息窗格中。
您可以使用與要傳遞的內容綁定的CommandParameter
屬性。
<Button Content="Edit" Command="{Binding Path=EditCommand}" CommandParameter="{Binding SelectedItem, ElementName=datagrid}" />
datagrid
是您提到的Datagrid。
進入你的CONTAINER
xaml並寫
<CONTAINER.Resources>
<DataTemplate DataType="{x:Type vm:UserViewModelVM}">
<!--if your View is a separat Usercontrol-->
<vw:UsertV />
<!--else insert your UsertV xaml-->
</DataTemplate>
</CONTAINER.Resources>
此xaml告訴您的應用程序,如果直接顯示UserViewModelVM
,則必須將其包裝在DataTemplate
現在您轉到LEFT PANE USER CONTROL
<LEFT PANE USER CONTROL Content={Binding yourVMProperty}
DataContextContent={Binding yourVMProperty}/>
公開UserViewModelVM
我們將其綁定為Content,現在應該可以正常工作
也不要忘了RaisPropertyChanged並刪除折疊的,因為不再需要隱藏它了:)
問我是否需要進一步的解釋。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.