使用 MVVM 在 wpf 中的父窗口内打开子窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7446825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
open child window inside a parent window in wpf using MVVM
提问by Shai
I have a window called "DashBoard" window. This window appears after user sucessfully logs in. Now I wanted to open child window called "Memberlist"inside the Dashboard when user select an option from menu item. User should not be able to pullout "Memberlist" window from "Dashboard" window.
我有一个名为“DashBoard”的窗口。用户成功登录后会出现此窗口。现在我想在用户从菜单项中选择一个选项时在仪表板内打开名为“Memberlist”的子窗口。用户应该无法从“仪表板”窗口中拉出“成员列表”窗口。
DashboardView.xmal (Parent Window)
DashboardView.xmal(父窗口)
<Window x:Class="MyProject.DashboardView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="My Project: Dashboard" Height="600" Width="800" WindowState="Maximized"
WindowStartupLocation="CenterOwner">
<Grid>
<Menu Height="23" HorizontalAlignment="Left" Name="menu1" VerticalAlignment="Top"
Width="44">
<MenuItem Header="_File" >
<MenuItem Header="View Memberlist…" Command="{Binding
Path=DisplayMemberlistCommand}" />
</MenuItem>
</Menu>
</Grid>
</Window>
MemberlistView.xaml (Child Window)
MemberlistView.xaml(子窗口)
<Window x:Class="MyProject.MemberlistView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="View Memberlist" Height="400" Width="806">
<Grid>
<Grid Height="383" Width="1179">
<toolkit:DataGrid x:Name="dgMemberlist"
ItemsSource="{Binding Path=MemberList}"
AutoGenerateColumns="False" Margin="21,57,422,106"
SelectedItem="{Binding Path=SelectedMemberItem,
UpdateSourceTrigger=PropertyChanged}">
<toolkit:DataGrid.Columns>
<toolkit:DataGridTextColumn Header="Export ID" Width="100"
Binding="{Binding MemberfullName}" IsReadOnly="True" />
</toolkit:DataGrid.Columns>
</toolkit:DataGrid>
</Grid>
</Grid>
</Window>
DashboardViewModel.cs
DashboardViewModel.cs
private ICommand _displayMemberlistCommand;
public ICommand DisplayMemberlistCommand
{
get
{
if (_displayMemberlistCommand == null)
_displayMemberlistCommand = new RelayCommand(a=>DoDisplayMemberlist(),
p=>true);
return _displayMemberlistCommand;
}
set
{
_displayMemberlistCommand = value;
}
}
private void DoDisplayMemberlist()
{
DashboardView dv = new DashboardView();
MemberlistView mlWindow = new MemberlistView ();
mlWindow.Owner = Application.Current.MainWindow;
mlWindow .Show();
}
回答by Rachel
I would recommend not referencing the Views in the ViewModel. Instead, create a MemberListViewModel
, and use a DataTemplate
to display it.
我建议不要在 ViewModel 中引用视图。相反,创建一个MemberListViewModel
,并使用 aDataTemplate
来显示它。
So your DashBoardViewModel
would have a
所以你DashBoardViewModel
会有一个
ViewModelBase CurrentView {get; set;}
ViewModelBase CurrentView {get; set;}
property, and on ShowMemberListCommand
simply sets
属性,并且在ShowMemberListCommand
简单的集合上
CurrentView = new MemberListViewModel();
CurrentView = new MemberListViewModel();
Your DashboardView
would contain
你DashboardView
会包含
<ContentControl Content="{Binding CurrentView}">
<ContentControl.Resources>
<DataTemplate DataType="{x:Type local:MemberViewModel}">
<local:MemberView />
</DataTemplate>
</ContentControl.Resources>
</ContentControl>
As long as CurrentView
is null
, the control is never visible. Once you execute the command to show the MemberView
, CurrentView
gets set to a MemberViewModel
and the ContentControl gets populated
只要CurrentView
是null
,控件就永远不可见。执行命令以显示MemberView
,CurrentView
将设置为 aMemberViewModel
并填充 ContentControl
回答by Shai
Since I did not know the technical term, it took me longer to find solution. I was refereeing to MDI (Multiple Document Interface). I found very good example with sample code on this URL http://wpfmdi.codeplex.com/
由于我不知道技术术语,所以我花了更长的时间才找到解决方案。我正在裁判 MDI(多文档界面)。我在这个 URL http://wpfmdi.codeplex.com/上找到了很好的示例代码示例
回答by Souvik Basu
Make MemberListView a UserControl instead of Window and add it to main window content on menu click.
使 MemberListView 成为 UserControl 而不是 Window 并在菜单单击时将其添加到主窗口内容。