如何通过添加新按钮在不同的 WPF 用户控件中重用菜单和工具栏? [英] How to reuse Menu and Toolbar in different WPF UserControls with adding new buttons?

查看:24
本文介绍了如何通过添加新按钮在不同的 WPF 用户控件中重用菜单和工具栏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将当前的 WinForms 应用程序移植到 WPF 并且需要帮助构建 WPF 类结构.

I'm trying to port current WinForms app to WPF and need help with building WPF class structure.

目前在 WinForms 中,我有一个 基类(带有菜单、工具栏、网格和上下文菜单)和几个具有不同数据源和网格列的 继承类菜单、工具栏和上下文菜单的附加按钮.

Currently in WinForms, I have one base class (with Menu, Toolbar, Grid and Context menu) and several inherited classes with different datasources and columns for the grid and additional buttons for menu, toolbar and context menu.

问题:

  • 对于我的用户控件避免继承的最佳 WPF 类结构是什么?

我可以将 ToolBar 移到 ResourceDictionary 中吗(例如 ControlTemplate)?
如果按钮将在单独的资源文件中指定,如何添加新按钮或将命令绑定到现有按钮?

Can I move ToolBar into ResourceDictionary (e.g. ControlTemplate)?
How to add new buttons or to bind commands to existed buttons, if buttons will be specified in a separate resource file?

还有其他想法吗?

由于 WPF 中的继承限制,我只看到了一种避免重复菜单、工具栏等的方法 - 仅在没有 XAML 的情况下在 C# 代码中实现基类.并且可能我也不能将 XAML 用于继承的类(不知道如何将 XAML 中的新按钮添加到在基类中创建的工具栏中)

Due to inheritance restrictions in WPF I see only one way to avoid duplicating Menu, Toolbar, etc. - implement base class in C# code only without XAML. And likely I can't use XAML for inherited classes as well (have no idea how to add new buttons in XAML into Toolbar created in base class)

推荐答案

在 WPF 中一个 Toolbar 是一个 ItemsControl(对于 Menu),因此它有一个 ItemsSource 属性,您可以将其绑定到您的工具栏项集合.

In WPF a Toolbar is an ItemsControl (same for Menu), so it has an ItemsSource property you can bind to your collection of toolbar items.

<Window.Resources>
    <DataTemplate x:Key="ItemTemplate1">
        <StackPanel>
            <TextBlock Text="{Binding Property1}"/>
        </StackPanel>
    </DataTemplate>
</Window.Resources>

<Grid x:Name="LayoutRoot" DataContext="{Binding MyViewModel}">
    <ToolBar HorizontalAlignment="Left" VerticalAlignment="Top" ItemTemplate="{DynamicResource ItemTemplate1}" ItemsSource="{Binding ToolbarItems}"/>
</Grid>

在这段代码中,ToolbarItems 是一个 ObservableCollection,其中 MyToolbarItem 是表示一个工具栏项的视图模型.

In this code, ToolbarItems is an ObservableCollection< MyToolBarItem >, where MyToolbarItem is a viewmodel that represents one toolbar item.

MyToolBarItem 可以是基类,有几个类继承自它.然后您可以使用 DataTemplateSelector 来使用不同的模板取决于它是工具栏项的类型.

MyToolBarItem could be the base class, with several classes inheriting from it. Then you can use a DataTemplateSelector to use a different template depending on the type of toolbar item it is.

这样,您的所有具有工具栏的用户控件都可以使用您在字典中定义的相同模板;每个工具栏只是绑定到不同的 MyToolBarItems 集合.

In this way, all your user controls that have a toolbar can use the same templates you define in your dictionary; each toolbar is just bound to a different collection of MyToolBarItems.

如果其中一些听起来让人难以接受,您可以阅读一些 MVVM.正是这种设计模式让 WPF 变得很棒.

If some of that sounds overwhelming, you can read up on some MVVM. It is the design pattern that makes WPF great.

这篇关于如何通过添加新按钮在不同的 WPF 用户控件中重用菜单和工具栏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆