111,097
社区成员




<Grid Grid.Column="2" >
<UserControl Content="{Binding CtrlChildren}"/>
</Grid>
<MenuItem Header="功能">
<MenuItem Header="销售" Tag="SellUserControl" Command="{Binding ShowChildrenWindowCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Tag}"/>
<MenuItem Header="采购" Tag="PurchaseUserControl" Command="{Binding ShowChildrenWindowCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Tag}"/>
<MenuItem Header="调拨" Tag="AllotUserControl" Command="{Binding ShowChildrenWindowCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Tag}"/>
<MenuItem Header="库存管理" Tag="StockUserControl" Command="{Binding ShowChildrenWindowCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Tag}"/>
<MenuItem Header="报表查询" Tag="ReportUserControl" Command="{Binding ShowChildrenWindowCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Tag}"/>
<MenuItem Header="用户管理" x:Name="User" Tag="UserUserControl" Command="{Binding ShowChildrenWindowCommand}"
CommandParameter="{Binding ElementName=User, Path=Tag}"/>
</MenuItem>
<MenuItem Header="功能">
<MenuItem Header="销售" Tag="SellUserControl" Command="{Binding ShowChildrenWindowCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Tag}"/>
<MenuItem Header="采购" Tag="PurchaseUserControl" Command="{Binding ShowChildrenWindowCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Tag}"/>
<MenuItem Header="调拨" Tag="AllotUserControl" Command="{Binding ShowChildrenWindowCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Tag}"/>
<MenuItem Header="库存管理" Tag="StockUserControl" Command="{Binding ShowChildrenWindowCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Tag}"/>
<MenuItem Header="报表查询" Tag="ReportUserControl" Command="{Binding ShowChildrenWindowCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Tag}"/>
<MenuItem Header="用户管理" x:Name="User" Tag="UserUserControl" Command="{Binding ShowChildrenWindowCommand}"
CommandParameter="{Binding ElementName=User, Path=Tag}"/>
</MenuItem>
在ViewModel中
public class MainViewModel : NotificationObject
{
#region 数据属性
private UserControl ctrlChildren;
public UserControl CtrlChildren
{
get { return ctrlChildren; }
set
{
ctrlChildren = value;
this.NotifyPropertyChanged("CtrlChildren");
}
}
#endregion
#region 命令属性
public ICommand ShowChildrenWindowCommand
{
get
{
return new DelegateCommand(p =>
{
this.CtrlChildren = GetChildren(p);
});
}
}
#endregion
#region 辅助方法
private UserControl GetChildren(object ctrl)
{
Type type = App.Current.MainWindow.GetType();
Assembly assembly = type.Assembly;
string userControlFullName = string.Format("{0}.{1}", type.Namespace, ctrl.ToString());
UserControl userCtrl = (UserControl)assembly.CreateInstance(userControlFullName);
return userCtrl;
}
#endregion
}
<Button Content="打开A窗口" Command="{Binding MyCommand}" CommandParameter="A"/>
<Button Content="打开B窗口" Command="{Binding MyCommand}" CommandParameter="B"/>
在ViewModel中大概是这样:
public ICommand OpenWindowCommand
{
get
{
return new RelayCommand<string>(p=> {
if (p=="A") OpenWindowA();
else OpenWindowB
}
}
}