如何动态获取命令并获取结果 [英] How to dynamically get the command and get the result

查看:58
本文介绍了如何动态获取命令并获取结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,有一个功能区控件,并且在该控件中,有一个保存"按钮,就像QuickAcessButton一样,例如保存,撤消,重做等....

我在这些使用控件中拥有如此多的用户控件,因此它具有网格控制或某些文本框,我必须将所有数据保存在特定用户控件的特定实体中.,


像一个用户控件具有雇员的基本信息,而一个用户控件具有与银行相关的信息等.因此,当我单击功能区控件的保存"按钮时,我想将垂直用户控件的值保存到垂直实体表中...

请给我一些建议...

in my project there is a Ribbon Control and in this control there is Save button for all just like QuickAcessButton like save, undo, redo etc. etc....

i hv so many user control in these use control it has grid contrl or some textbox throught this i have to save all the data in perticular entity of a perticular usercontrol.,,


like one user control has Basic info of employee, and one user control has Bank related info etc.. so when i click the Save button of ribbon control i want to save the perticular user control value to a perticular entity table ...

plz give me some suggestion...

推荐答案



首先,您可以看一下以下内容:创建通用实体框架4.0存储库 [ ^ ]-这将允许您创建一个通用存储库,以容纳任何实体表类.

其次,如前所述,您可以执行以下操作:

我不知道您是使用MVVM还是任何其他设计模式,因此这将是一个非常基本的示例:

我们的界面:

Hi,

Well first of all, you could have a look at the following: Creating a Generic Entity Framework 4.0 Repository[^] - this will allow you to create a generic repository which will accommodate any entity table class.

Secondly, as mentioned earlier you could do something like this:

I don''t know if you are using MVVM or any other design pattern, so this is going to be a very basic example:

Our Interface:

public interface ISavable
{
    int SaveData();
}



我们的第一个UserControl-实现了 ISavable :



Our first UserControl - which implement ISavable:

public partial class UserControl1 : UserControl, ISavable
{
    public UserControl1()
    {
        InitializeComponent();
    }

    #region ISavable Members

    public int SaveData()
    {
        using (DevelopmentEntities context = new DevelopmentEntities())
        {
            context.Events.AddObject(new Event());
            return context.SaveChanges();
        }
    }

    #endregion
}



我们的第二个UserControl-实现了 ISavable :



Our second UserControl - which implements ISavable:

public partial class UserControl2 : UserControl, ISavable
{
    public UserControl2()
    {
        InitializeComponent();
    }

    #region ISavable Members

    public int SaveData()
    {
        using (DevelopmentEntities context = new DevelopmentEntities())
        {
            context.EventTypes.AddObject(new EventType());
            return context.SaveChanges();
        }
    }

    #endregion
}



现在,在我们的主类中(功能区控件被实例化的任何地方),我们需要将 active用户控件强制转换为该接口并调用适当的方法-如前所述,这是一个非常基本的示例,但演示了原理:



Now, within our main class (where ever the ribbon control is instantiated), we need to cast the active user control to the interface and call the appropriate method - as mentioned earlier, this is a very basic example, but demonstrates the principle:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        ISavable saveUserControl = this.DataContext as UserControl1;
        saveUserControl.SaveData();

        ISavable saveUserControl2 = this.DataContext as UserControl2;
        saveUserControl.SaveData();
    }
}



不用说,您可以通过使其更通用以及实现MVVM模式来扩展它.

希望对您有帮助!
亲切的问候,



Needless to say you could extend this by making it more generic as well as implementing a MVVM pattern.

Hope it helps!
Kind regards,


这篇关于如何动态获取命令并获取结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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