MVVM 中一个Command执行多个任务

TaoRenYan 2014-05-02 05:43:38
在WPF 的MVVM中 我只用一个Command 要执行多个任务。 比如我的一个OpenChildrenWindowCommand 当我点击“打开A窗口”时 ,显示的是A窗口 当我点击“打开B窗口”时 ,显示的是B窗口 这些都是调用同一个OpenChildrenWindowCommand 命令 只是调用的参数不同,就显示不同的窗口 以前写过的,现在竟然写不出来了。那位能帮下?
...全文
134 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
TaoRenYan 2014-05-04
  • 打赏
  • 举报
回复
要加入用户控件的Grid
<Grid Grid.Column="2" >
                <UserControl Content="{Binding CtrlChildren}"/>
            </Grid>
TaoRenYan 2014-05-04
  • 打赏
  • 举报
回复
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>  
TaoRenYan 2014-05-04
  • 打赏
  • 举报
回复
问题已经解决: 在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
    }
bwangel 2014-05-02
  • 打赏
  • 举报
回复
在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
}
}
}

111,097

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧