Click here to Skip to main content
15,883,883 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hai,

please be patience to read this...

briefly what i want is " when i
click on the DataGrid row those values have to be displayed in another
form(here userControl).."


I am using one Wpf Ribbon window.. And that window is divided in to two rows. In first row i
filled with Wpf Ribbon and in Second row i filled one Dock panel to fill the Usercontrols based on
the Action..

The Ribbon Has Two Button 1)Display All employees 2) To Add Employee

i created two Usercontrol Forms one is to enter the employee Details , second one is to Display
all the Records in DataGrid..

Now i First Click on Ribbon Button1 to Display all the Employee Details ,then userControl
"CtlGridDisplay "is loaded in to the DockPanel(which is in Ribbon Window) and displaying the all the
Details in DataGrid.

Now i Double Click on row and take the selected row employeeId And then i wish to Display that
Details in Another Usercontrol "CtlAddEmployee" And this userControl has to be Displayed in that
DockPanel only.. for that i trying to Clear the Dockpanel and created the object for the Control
"CtlAddEmployee" and added that control to the DockPanel.. But the DockPanel Remains fill with
DataGRid only i.e filled with "CtlGridDisplay" ... i can't understand why...


To do this i have also used the Events and Delegates ,it executes all the code well but the
Dockpanel is not cleared and didn't loaded with new control..

But when i click one ribbon button1 it loads the "CtlGridDisplay" and when i click on Ribbon
button2, its loading "CtlAddEmployee" in Dock panel well...

but when i trying to display the Data what i have click on DataGrid in one userControl to display in Another control doesn't Work..

Events Delegate approach Code is like below...


C#
    In User Control "CtlDisplayEmployeeList " the Code is 


     public event MyOrganizationDetails.MainRibbonWinodw.DisplayEmployeeHandler DisplayemployeeEvent;

     private void GridList_MouseDoubleClick(object sender, MouseButtonEventArgs e)
            {
                 MainRibbonWinodw mainRibbonobj = new MainRibbonWinodw();
                 this.DisplayemployeeEvent += new MainRibbonWinodw.DisplayEmployeeHandler(mainRibbonobj.fnDisplayEmployeeDetails);
                if (GridList.SelectionUnit == DataGridSelectionUnit.FullRow)
                {
                    if (GridList.SelectedItems.Count > 0)
                    {
                        for (int i = 0; i < GridList.SelectedItems.Count; i++)
                        {
                            DataGrid dg = sender as DataGrid;
                            EmployeeDetail detail = dg.SelectedItem as EmployeeDetail;
                            string selectedEmp = detail.EmpId; //it gives Employee Id
    
    
                            if (DisplayemployeeEvent != null)
                                DisplayemployeeEvent(selectedEmp);
    
    
                        } 
                    }
                }
            }
    
    
    
    In Ribbon Window.. the Code will be... which handles the Event here...
    
    
     public partial class MainRibbonWinodw : RibbonWindow
        {
    
            public delegate void DisplayEmployeeHandler(string str);
    
    
     public void fnDisplayEmployeeDetails(string str)
            {
    
                CtlAddEmployee frm2 = new CtlAddEmployee(str);
                DockPanelInRibbon.Children.Clear();
                DockPanelInRibbon.Children.Add(frm2); 
    
                }
            }
<pre lang="sql">In another User Control..

Constructor...



 public CtlAddEmployee(string str)
        {
            InitializeComponent();
            fnDisplayingEmployee(str);

        } 


when i debugging all the Code is fired well but "CtlAddEmployee" form is not loaded to DockPanel... the Dock panel remains with that DataGRid... i Don't know why please tell me the solution .....

Thank you for your patience...to read this...

Thank you
Posted

1 solution

You are not notifying the UI that anything has changed. You will need to clear and add the usercontrol using the dispatcher.begininvoke method. This executes the method on the UI thread and updates the screen.

Can I also recommend you look into command binding and event aggregation. Using an event aggregator, messenger or mediator pattern will help very much when communicating with multiple controls on different windows.

If you also take a look at MVVM pattern and maybe a framework like MVVM light or Prism, these have great things in it for communicating!

event aggregator pattern:
http://martinfowler.com/eaaDev/EventAggregator.html[^]

excellent mediator pattern
http://sachabarbs.wordpress.com/2009/03/16/mvvm-mediator-pattern/[^]
 
Share this answer
 
Comments
Sandeep Mewara 31-May-12 16:20pm    
Comment from OP:
Thank you For your Reply...

Can you please tell me any site which has implemented dispather.beginInvoke method so that to clear the usercontrol...

i used ---->
this.Dispatcher.Invoke((Action)delegate { this.stkpRibbonContent.Children.Clear(); }, null);
To Clear the DockPanel..but it didn't effect this...

Thank you...
db7uk 31-May-12 16:50pm    
Ok, let me get this straight. You have a window with a ribbon at the top and a dockpanel at the bottom.

The dockpanel will get the ctrlAllEmployees control when you click the button on the ribbon.

When a user double clicks on an employee you want the dockpanel in the window to switch views and load the ctrlAddEmployee control?

You need to use a central location, a controller if you like which would control the view switch. This controller should reside in your application so that all windows and controls can access it. This is where the mediator, event aggregator or even a custom implementation using a singleton would come in.

Your double click event on the grid needs to register, raise or publish the double click event so that the controller can switch views. The controller would clear all child controls from the dockpanel and replace it with a new control.

To do this you will need to action it on the UI thread and this is where the Dispatcher.Invoke / beginInvoke is used.
V G S Naidu A 1-Jun-12 0:12am    
Thank You Very Much ...
i need to implement this.. i am beginner in wpf...
please Suppose me if i had any doubts...without lose patience...

once again thank you very much...

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