1

I have a windows form application and one of the control on the form is WPF user control. I have put element host control on the windows form and this is loading my user control at startup.

What I want is to load and refresh user control at certain time. As I need fresh data to be loaded.

I have tried elementHost1.Refresh(); and this is not doing anything.

How can I load and refresh this?

This is my user control on this LINK

Community
  • 1
  • 1
user123_456
  • 5,635
  • 26
  • 84
  • 140
  • Guys you can simply try this if you make new windows form project and insert my code and rss lnk and try to refresh it on button click. – user123_456 May 31 '12 at 22:01
  • I did exactly that. Inserted a (Windows Forms) button into your windows form and added my two lines of code which you can see below into the button click handler. Believe me or not, it works. – JohnB Jun 01 '12 at 05:29

4 Answers4

2

Make data provider re-read its source:

var dataprovider = (System.Windows.Data.XmlDataProvider) (
  ((UserControl1) (elementHost1.Child)).Resources ["rssData"]
);
dataprovider.Refresh ();
JohnB
  • 13,315
  • 4
  • 38
  • 65
0

What happens when you call your load event again?

Or at least the methods you are using to populate your data in the form>

i.e. either call your this.Load(null,null) (or pass some objects or events as needed)

private void form1_load(object o, EventArgs e)
{
    BindDataToControlMethod();
}

or have your code refactored so you can just call your BindDataToControlMethod() method(s).

The elementHost1.Refresh() call you are making just redraws the form on the screen. It does not reload per se.

Brad
  • 11,934
  • 4
  • 45
  • 73
  • this is a control that is loaded trough toolbox and this usercontrol is just as part of my screen which has a rss data and then I have a button for example which will be used as refresh. I don't want to load all form just only small port of it which calls usercontrol. That's the problem. If this is a full screen mode then I would load all form and done. – user123_456 May 30 '12 at 19:58
  • I think you can apply the same logic to a usercontrol as to the entire form. Both are objects which can be reinstantiated. If you need to filter your results when you make your call you'll have to do that on your own because there is not enough information here for me to try. – Brad May 30 '12 at 20:07
  • tell me what do you need as I don't understand exactly what you are saying. So I have one button and usercontrol which is loaded in elementHost1 trough the toolbox on windows form designer. That's all I have as I want to try how it works. Do you need a usercontrol code? – user123_456 May 30 '12 at 20:09
  • when your form loads, you then load your ElementHost. Within that you load your personally generated userControl. Is that right so far? How do you get your data into your usercontrol when it loads? – Brad May 30 '12 at 20:21
  • I have wrote usercontrol and I have made the file and it is in my project. I have added the elementhost from toolbox and it has a option to load wpf file if you have created one. So basically this is loaded when form starts. Data are loaded before as I am loading Drom RSS link – user123_456 May 30 '12 at 20:31
  • are you saying that you're loading the data at designtime NOT runtime? – Brad May 30 '12 at 20:33
  • I can try this again at home. I don't have Visual Studio in front of me. – Brad May 30 '12 at 20:38
  • Okay please do.. When can you do it? – user123_456 May 30 '12 at 20:42
0

What about MVVM and INotifyPropertyChanged? Using INotifyPropertyChanged you can notify View when data has been changed and refresh it in this way. Seems like this is what you want.


Take a look into couple related links:

Community
  • 1
  • 1
Anatolii Gabuza
  • 6,184
  • 2
  • 36
  • 54
  • That are really good articles. But this is for user form writtenin the c#.Imhave already created form as user control which is only loaded into elementhost control. I want to refresh it when button is pressed. – user123_456 May 31 '12 at 21:41
  • I have added the link where you can see the code of the user control – user123_456 May 31 '12 at 21:47
0

As your Control is not refreshed automatically, I assume that you do not use data binding, which might be the best solution.

Suppose you have a user control with (custom) type UserControl1 hosting a button named MyButton.

You could either create a new WPF control and assign it to the child element, e.g.

((UserControl1) (elementHost1.Child)).MyButton = new System.Windows.Button ()

or you access the WPF control as follows

var wpfButton = ((UserControl1) (elementHost1.Child)).MyButton;

and then simply reset the necessary properties of the WPF control:

wpfButton.Content = "My new text";

EDIT: Mistakes corrected.

JohnB
  • 13,315
  • 4
  • 38
  • 65
  • I have data binding as this user control is rss reader so I have data binding in it. I just want to refresh when button is clicked – user123_456 May 31 '12 at 21:43
  • I have added the link where you can see the code of the user control – user123_456 May 31 '12 at 21:47
  • But then you will probably want to make the XML data provider update, not one of your controls??? Give the data provider a name, access it via elementHost1.Child ["The name of the data provider"], cast this to an XmlDataProvider and call Refresh () on the data provider! See my other post. – JohnB May 31 '12 at 22:04