WPF UI 场景 - 在 50 个视图中添加功能的最佳方式?

发布于 2024-10-11 13:52:42 字数 344 浏览 3 评论 0原文

我想要一些建议,以简洁的设计实现此功能,并且无需任何代码复制。我有一个具有许多视图和大多数视图中的网格控件的应用程序。我需要添加一个导出功能(将记录导出到excel)。网格控件支持此OOB,只需要调用'Grid.Export()'。我计划在每个网格的侧面放置一个 UI 按钮并调用此方法。

因此,显然我只需要在代码隐藏中编写代码,因为我需要控件的实例来调用该方法。但是,我喜欢将代码保留在一处,并以某种方式从所有 Xamls 调用代码。 (所有 WPF 视图)。

一种技术是编写一个 BaseView 类并从中派生所有视图。

但想知道 WPF 是否支持任何可以实现此目的的技术。 (行为等..?)

谢谢, 玛尼

I want some suggestions to implement this functionality with a neat design and without any code replication. I have an application with many views and grid control in most of the views. I need to add an export functionality (export records to excel).The grid control supports this OOB, just need to call 'Grid.Export()'. I am planning a UI button on the side of every grid and call this method.

So, obviously I need to write the code in code-behind only since I need the control's instance to invoke the method. But, I like to keep the code in one place and somehow invoke the code from all Xamls. (all WPF views).

One technique is to write a BaseView class and derive all Views from this.

But would like to know if WPF suppots any techniques by which I can achieve this. (behaviours etc..?)

Thanks,
Mani

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

治碍 2024-10-18 13:52:42

创建一个包含数据网格和导出按钮的UserControl。实际上,使其成为网格本身的一部分。

在所有视图中使用此UserControl而不是默认数据网格,就完成了。

此外,如果您必须修改按钮的外观和感觉或其行为,您只能在一处进行更改,并且它将在您的所有视图中更新。

Create a UserControl that includes both the datagrid and the export button. In effect, make it part of the grid itself.

Use this UserControl instead of the default datagrid in all of your views, and you're done.

Furthermore, if you ever have to modify the look and feel of your button or its behaviour, you have only one place in which to change it, and it will be updated in all of your views.

蝶舞 2024-10-18 13:52:42

解决方案之一是使用 WPF 路由命令。

注意:我写这个答案时假设您的“View”是 Window 类的子类。

首先,向您的项目添加自定义路由命令。

public static class MyCommands
{
    private static readonly RoutedUICommand exportCommand = new RoutedUICommand("description", "Export", typeof(MyCommands));

    public static RoutedUICommand ExportCommand
    {
        get
        {
            return exportCommand;
        }
    }
}

在每个视图中,将自定义命令设置为 Button.Command 并将目标对象绑定到 Button.CommandTarget。

<Button Command="local:MyCommands.ExportCommand" CommandTarget="{Binding ElementName=dataGrid1}">Export</Button>

最后,在您的 Application 类(默认情况下名为 App)中,注册自定义命令和 Window 之间的命令绑定。

public partial class App : Application
{
    public App()
    {
        var binding = new CommandBinding(MyCommands.ExportCommand, Export, CanExport);
        CommandManager.RegisterClassCommandBinding(typeof(Window), binding);
    }

    private void Export(object sender, ExecutedRoutedEventArgs e)
    {
        // e.Source refers to the object is bound to Button.CommandTarget.
        var dataGrid = (DataGrid)e.Source;

        // Export data.
    }

    private void CanExport(object sender, CanExecuteRoutedEventArgs e)
    {
        // Assign true to e.CanExecute if your application can export data.
        e.CanExecute = true;
    }
}

现在,当用户单击按钮时,将调用 App.Export。

示例可在此处获取。

One of solutions is to use WPF routed command.

Note: I wrote this answer with the assumption that your "View" is a subclass of Window class.

First, add a custom routed command to your project.

public static class MyCommands
{
    private static readonly RoutedUICommand exportCommand = new RoutedUICommand("description", "Export", typeof(MyCommands));

    public static RoutedUICommand ExportCommand
    {
        get
        {
            return exportCommand;
        }
    }
}

In each View, set your custom command to Button.Command and bind a target object to Button.CommandTarget.

<Button Command="local:MyCommands.ExportCommand" CommandTarget="{Binding ElementName=dataGrid1}">Export</Button>

Firnally, in your Application class (named App by default), register a command binding between your custom command and Window.

public partial class App : Application
{
    public App()
    {
        var binding = new CommandBinding(MyCommands.ExportCommand, Export, CanExport);
        CommandManager.RegisterClassCommandBinding(typeof(Window), binding);
    }

    private void Export(object sender, ExecutedRoutedEventArgs e)
    {
        // e.Source refers to the object is bound to Button.CommandTarget.
        var dataGrid = (DataGrid)e.Source;

        // Export data.
    }

    private void CanExport(object sender, CanExecuteRoutedEventArgs e)
    {
        // Assign true to e.CanExecute if your application can export data.
        e.CanExecute = true;
    }
}

Now, App.Export is invoked when user click a button.

Sample is available here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文