0

I am trying to create a usercontrol in WPF to show in a windows forms application using visual studio. I've followed a tutorial or two but think I am getting the concepts wrong.

I am trying to use WPF as a control designer and handle it's functionality from my Windows forms application.

I have created a WPF user control, consisting of a shaded rectangle with a text block over it, and a couple of buttons.

I've then added the control to my form by dragging it from the toolbox to the canvas.

How do I then detect and handle the button-presses on the control, or write text to the textblock on the control? I can't seem to reference them from the windows forms application.

-- Steps to reproduce what I have so far.....

  1. Create new vb.net forms project.
  2. From Solution Explorer, add new item -> WPF User Control
  3. Draw 2 buttons (named myButton1 and myButton2)
  4. Draw TextBlock (named myTextBlock)
  5. Compile Project
  6. Drag UserControl1 from WPFTestApp WPF User Controls onto form 1

That now displays correctly, but how do I add events to the buttons, or write data back to the text block?

Peterp
  • 100
  • 13
  • It would help if you would show us some code. – Mighty Badaboom Mar 30 '17 at 10:27
  • 1
    you are doing this the wrong way round really, While WindowsFormsIntegration.dll, provides ElementHost that allows you to use WPF controls on a Winforms Application WPF is the more advanced technology so you should consider moving entirely onto WPF – MikeT Mar 30 '17 at 10:42
  • @MikeT how does your comments address the OP issues? Although I agree WPF is the way to go ***if*** you can. For example I'm working on a legacy program and it's currently getting a huge facelift, slowly converting usercontrols to WPF which some are still smacked on winform containers because other controls haven't been fully converted yet... – Trevor Mar 30 '17 at 11:11
  • Fortunately, @MikeT's comment was useful. I hadn't realised that I could code VB in a WPF application. I have been writing vb.net applications for a couple of years and never looked at WPF. I had mistakenly thought WPF was just for designing interfaces (using Blend) – Peterp Mar 30 '17 at 11:19
  • You didn't know you could program in wpf with vb? When you create a WPF app you pick a template ***which*** tells you the language... – Trevor Mar 30 '17 at 11:38
  • @Zaggler thats why it was a comment not an answer, it didn't answer their question but was a comment that i thought they were doing the wrong thing – MikeT Mar 30 '17 at 12:48
  • 1
    The problem is that you have eclosed your WPF in a usercontrol with out exposing the behaviour you need see [Expose all event handlers of UserControl](http://stackoverflow.com/questions/25918061/expose-all-event-handlers-of-usercontrol) – MikeT Mar 30 '17 at 14:01

0 Answers0