【问题标题】:WPF ToolBarControl in multiple UserControls多个用户控件中的 WPF ToolBarControl
【发布时间】:2017-03-03 12:30:53
【问题描述】:

我正在处理一个项目,该项目的请求是在多个 UserControls 中实现 ToolBarControlUserControl 主要有那个工具栏和GridView (Devexpress)。 我正在使用带有 MVVM 和 Caliburn.Micro 框架的 WPF 进行开发。

问题是,我需要在XAML 中c/p 代码ToolBarControl 然后在ViewModel 中实现属性。

我正在寻找更好的方法,现在我想这将是反思。 任何建议都会有所帮助,代码示例也是如此。

更新 #2

自定义工具栏中的控件应该能够向上或向下移动选定的行、删除项目、编辑和创建(最后两个应该打开一个新窗口)。 假设我有CustomersListViewModel,它的CustomersListView 中有自定义ToolBarControlGridControl。 当我单击添加按钮时,它应该会打开我的 CustomersEditViewModel。 当我单击删除时,它应该删除列表中的选定项目。 当我点击向上移动时,它应该向上移动选定的行。

【问题讨论】:

    标签: c# wpf xaml mvvm user-controls


    【解决方案1】:

    您可以在 app.xaml 中使用数据模板工具栏视图模型和工具栏视图,然后使用内容控件显示工具栏,将其绑定到工具栏视图模型的实例

    app.xaml:

    <ResourceDictionary>
     <DataTemplate DataType="{x:Type ViewModelToolBar}">
            <startViews:ViewToolBar />
        </DataTemplate>
     </ResourceDictionary>
    

    在您的用户控件中:

    <ContentControl Content="{Binding MyViewModelToolBar}"/>
    

    并执行您的命令,您可以使用带有标签或参数的通知事件来告诉您的用户控件视图模型应该执行哪些操作。意味着您将工具栏按钮绑定到通知命令并使用按钮名称或标签作为参数。

    ViewModelToolBar:

     public event EventHandler Notify;
    
        private void OnNotify(object sender)
        {
            Notify?.Invoke(sender, new EventArgs());
        }
    
        public ICommand NotifyCommand => new DelegateCommand<object>(OnNotify);
    

    在您的用户控件 ViewModel 中:

     MyViewModelToolBar = new ViewModelToolBar();
     ViewModelToolBar.Notify += ViewModelToolBar_Notify;
    
      private void ViewModelToolBar_Notify(object sender, EventArgs e)
            {
                switch (sender.ToString())
                {
                    case "Case1":
                       "perform your operation"
                        break;
                    case "Case2":
                       ...
                        break;
                    case "Case3":
                       ...
                        break;
                                  }
            }
    

    【讨论】:

    • 如何将它绑定到我的GridControl
    • 带有 switch 语句的部分应该在 viewmodel 中,您可以在其中编辑 CustomersEditViewModel 数据
    • 这意味着在每个包含我的自定义工具栏的UserControl 中,我需要实现 switch 语句......不是吗?
    • 您是否在您的 CustomersListViewModel 中创建您的 CustomersEditViewModel 实例?然后您可以在 CustomersListViewModel 中创建 ViewModelToolBarModel 并将其作为参数传递给您的 CustomersEditViewModel,例如
    • 你总是对相同的 ViewModel 类型使用相同的操作吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-08
    • 2018-01-16
    • 1970-01-01
    相关资源
    最近更新 更多