我有一个名为“DashBoard”窗口的窗口。用户成功登录后会出现此窗口。现在,当用户从菜单项中选择一个选项时,我想在仪表板中打开名为“Memberlist”的子窗口。用户不能从“Dashboard”窗口中拉出“Memberlist”窗口。
<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>
<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>
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();
}
答案 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>
只要CurrentView
为null
,控件就永远不可见。执行命令以显示MemberView
后,CurrentView
设置为MemberViewModel
并且ContentControl将填充
答案 1 :(得分:1)
由于我不知道技术术语,我花了更长的时间才找到解决方案。 我正在裁判MDI(多文档界面)。我在此网址http://wpfmdi.codeplex.com/
上找到了示例代码的非常好的示例答案 2 :(得分:0)
使MemberListView成为UserControl而不是Window,并在菜单点击时将其添加到主窗口内容。