Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WPF
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 25-Nov-12 22:37pm
Comments
Programm3r at 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.
kitts.enziq at 26-Nov-12 7:26am
   
can u plz give any basic example...

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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();
}
 
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
}
 
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
}
 
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();
        }
    }
 
Needless to say you could extend this by making it more generic as well as implementing a MVVM pattern.
 
Hope it helps!
Kind regards,
  Permalink  
v2

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



Advertise | Privacy | Mobile
Web04 | 2.8.141022.1 | Last Updated 26 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100