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

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

问题描述

我试图将当前的 WinForms 应用程序移植到 WPF ,并需要帮助构建 WPF 类结构。



目前在WinForms中,我有一个基类(带有Menu,Toolbar,Grid和Context菜单)和几个带有不同数据源的继承类和网格列以及用于菜单,工具栏和上下文菜单的其他按钮。

问题:


  • 为了避免继承,我的usercontrol中最好的WPF类结构是什么?


  • 将ToolBar移动到ResourceDictionary中(例如: ControlTemplate )?
    如果添加新按钮或将命令绑定到已存在的按钮,一个单独的资源文件?


  • 任何其他想法?


    由于 WPF中的继承限制,我只能看到一种避免重复使用Menu,Toolbar等的方法。 - 在C#代码中实现基类没有 XAML 。并且很可能我不能将XAML用于继承类(不知道如何将XAML中的新按钮添加到基类中创建的工具栏中)

    解决方案

    在WPF中,工具栏是一个ItemsControl(与菜单相同) ,所以它有一个ItemsSource属性,你可以将它绑定到你的工具栏项目集合。

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

    < Grid x:Name =LayoutRootDataContext ={Binding MyViewModel}>
    < ToolBar Horizo​​ntalAlignment =LeftVerticalAlignment =TopItemTemplate ={DynamicResource ItemTemplate1}ItemsSource ={Binding ToolbarItems}/>
    < / Grid>

    在这段代码中,ToolbarItems是一个ObservableCollection< MyToolBarItem>,其中MyToolbarItem是代表一个工具栏项目的视图模型。



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



    通过这种方式,所有具有工具栏的用户控件都可以使用您在字典中定义的相同模板;每个工具栏都绑定到不同的MyToolBarItems集合。



    如果有些听起来不可思议,您可以阅读一些 MVVM 。这是让WPF棒极了的设计模式。


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

    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.

    Questions:

    • What will be the best WPF class structure for my usercontrol to avoid inheritance?

    • 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?

    • Any other ideas?

    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)

    解决方案

    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>
    

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

    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.

    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.

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

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

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