MVVM 中一个Command执行多个任务 Command MVVM 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 在xaml中: <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}}} 问题已经解决:在Xaml中<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 } Xaml<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> 要加入用户控件的Grid<Grid Grid.Column="2" > <UserControl Content="{Binding CtrlChildren}"/> </Grid> winform如何解析JSON数据存储在Hash table中 求代码 必给分 用C#怎么样开发下面这样的控件 匹配首个出现的字符 [求教]请教ASP.NET中可以根据显示的字符长度自动换行的控件 怎么我的vs2008装了几个皮肤控件后,msdn帮助就没法用了? textbox进行数据绑定后,存盘时还要不要手动赋值? winfrom怎么使窗体变大,控件的位置也跟着原来设计的比例移动? 错误 @涉及“Form1”和“Form1”的循环基类依赖项 是什么意思呀? 今天发工资,喜悦中夹杂了一件气人的事!!!! 怎样在C#文件包含另一C#文件的内容? C#中窗体问题 WCF文件上传问题
<Button Content="打开B窗口" Command="{Binding MyCommand}" CommandParameter="B"/>在ViewModel中大概是这样:public ICommand OpenWindowCommand
{
get
{
return new RelayCommand<string>(p=> {
if (p=="A") OpenWindowA();
else OpenWindowB
}
}
}
在Xaml中
<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
}
<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>
<UserControl Content="{Binding CtrlChildren}"/>
</Grid>