使用MVVM在wpf中的父窗口内打开子窗口

时间:2011-09-16 15:17:18

标签: wpf mvvm

我有一个名为“DashBoard”窗口的窗口。用户成功登录后会出现此窗口。现在,当用户从菜单项中选择一个选项时,我想在仪表板中打开名为“Memberlist”的子窗口。用户不能从“Dashboard”窗口中拉出“Memberlist”窗口。

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(子窗口)

<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

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();
        }

3 个答案:

答案 0 :(得分:3)

我建议不要在ViewModel中引用视图。而是创建一个MemberListViewModel,并使用DataTemplate来显示它。

所以你的DashBoardViewModel会有一个

ViewModelBase CurrentView {get; set;}

属性,ShowMemberListCommand只需设置

CurrentView = new MemberListViewModel();

您的DashboardView将包含

<ContentControl Content="{Binding CurrentView}">
    <ContentControl.Resources>
        <DataTemplate DataType="{x:Type local:MemberViewModel}">
            <local:MemberView />
        </DataTemplate>
    </ContentControl.Resources>
</ContentControl>

只要CurrentViewnull,控件就永远不可见。执行命令以显示MemberView后,CurrentView设置为MemberViewModel并且ContentControl将填充

答案 1 :(得分:1)

由于我不知道技术术语,我花了更长的时间才找到解决方案。 我正在裁判MDI(多文档界面)。我在此网址http://wpfmdi.codeplex.com/

上找到了示例代码的非常好的示例

答案 2 :(得分:0)

使MemberListView成为UserControl而不是Window,并在菜单点击时将其添加到主窗口内容。