Click here to Skip to main content
15,887,999 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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...
Posted
Comments
Programm3r 26-Nov-12 5:15am    
Hi,
Just a suggestion, but you could implement an interface on each of the user controls added to the ribbon. When the button is clicked you can cast the user control to that interface and call the appropriate method.
Work - Dedication - Attitude 26-Nov-12 7:26am    
can u plz give any basic example...

1 solution

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:

C#
public interface ISavable
{
    int SaveData();
}


Our first UserControl - which implement ISavable:

C#
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
}


Our second UserControl - which implements ISavable:

C#
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
}


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:

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

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

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


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

Hope it helps!
Kind regards,
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900