2 Replies - 11614 Views - Last Post: 01 April 2013 - 03:28 PM Rate Topic: -----

#1 johnyjj2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 24-July 09

[WPF][C#] raise event in one UserControl and handle in the other

Posted 01 April 2013 - 09:54 AM


I have two different UserControls. I want values from DataGrid in one UserControl to be updated when value chosen in ListView in the other UserControl changes.

I have created function to refresh values:

public void RefreshDataGrid()
   dgrNumericalParameters.DataContext = null; // we detach and attach to refresh values
   dgrNumericalParameters.DataContext = Mainwindow.allEAsSettingsList.Find(item2 => item2.EAName == Mainwindow.currentEAsSettingsName).EAParameters; // refresh

I haven't found better way of refreshing value so far, but that's not the main thing I want to solve now.

I shouldn't access RefreshDataGrid (that belongs to TestScenario UserControl) from AllTestScenarios (the other UserControl) directly. So I thought about raising event in AllTestScenarios and catching it in TestScenario.

How could I possibly do it?

First of all, where should I declare the event? Should it be in MainWindow? MainWindow is common for all UserControls.

I can declare it like this:

public event EventHandler TestScenarioChosenFromList;

Perhaps there is something wrong in the above line, as it is EventHandler and not Event (there is no such a thing available as Event, according to Visual Studio).

I try to raise it like this:

RaiseEvent(Mainwindow.TestScenarioChosenFromList, null);

I guess that's the other incorrect thing, as I directly call MainWindow, and even if it is element common to all UserControls, it looks like directly accessing GUI what I should avoid).

How can I cause DataGrid from one UserControl to refresh when current element in ListView in the other UserControl is changed?

Best regards!

Is This A Good Question/Topic? 0
  • +

Replies To: [WPF][C#] raise event in one UserControl and handle in the other

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 6108
  • View blog
  • Posts: 21,024
  • Joined: 05-May 12

Re: [WPF][C#] raise event in one UserControl and handle in the other

Posted 01 April 2013 - 03:18 PM

Get an event from whenever the current selected item in the ListView changes. When the even fires, refresh your DataGrid view. There are several tutorials floating around. Search for the term "master detail".
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6528
  • View blog
  • Posts: 14,433
  • Joined: 02-June 10

Re: [WPF][C#] raise event in one UserControl and handle in the other

Posted 01 April 2013 - 03:28 PM

[*]Q: ...get Form/class 'A' to make a change or talk to Form/class 'B'

I don't like my tutorials to look like crap. So here is a sneak peek at something I hastily threw together so you have a sample. I'll clean it up on my days off to be a proper tutorial.

Make a new WPF project titled "WpfOneOhOne".
Now you can just copy/paste the code below to your MainWindows.XML replacing all the existing code.

Make a new UserControl called PersonData
Again just copy/replace all of the XAML with this

In the C# code-behind for the Mainwindow.xaml.cs, copy/replace with this code:

In the PersonData.xaml.cs, copy/replace all the code with this:

This will give you a very simple Windows with a very simple UserControl. Notice that as you type in the TextBox of the UserControl the TextBlock (yellow) of the MainWindow is updating in real time.


The key points here is that nothing is directly accessing the .Text property of anything else.
Binding is used to related GUI controls to properties.
You can see how ElementName and Path relate to the instance of the UserControl and property of that instance.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1