gpt4 book ai didi

c# - 多个 UserControl 中的 WPF ToolBarControl

转载 作者:太空宇宙 更新时间:2023-11-03 12:31:26 27 4
gpt4 key购买 nike

我正在从事一个项目,该项目要求在多个 UserControls 中实现 ToolBarControlUserControl 主要有工具栏和 GridView (Devexpress)。我将 WPF 与 MVVM 和 Caliburn.Micro 框架结合使用进行开发。

问题是,我需要在 XAML 中复制 ToolBarControl 的代码,然后在 ViewModel 中实现属性。

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

更新#2

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

最佳答案

您可以在您的 app.xaml 中使用数据模板 toolbarviemodel 和 toolbarview,然后使用 contentcontrol 来显示将其绑定(bind)到 toolbarviewmodel 实例的工具栏

应用程序.xaml:

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

在您的用户控件中:

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

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

View 模型工具栏:

 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;
}
}

关于c# - 多个 UserControl 中的 WPF ToolBarControl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42578947/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com