1

i referred this, wrote almost same code but in different langauge, its not working as expected!
i'm using winform(C++/Cli) as host & 'WPF User Control Libaray'(C#) as a child control.
integrated WPF user control(PictureBox) in winforms using ElementHost component in Winform.
Basically i wanted to change picture in WPF control from winform button. It compiles & runs fine.
But the only issue is that image is not changing even if the path for picture is proper.

below code is in winform button_click Event

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
    OpenFileDialog ^ofd = gcnew OpenFileDialog();
    WpfControlLibrary1::UserControl1 ^uc = gcnew UserControl1();
    if (ofd->ShowDialog() == Windows::Forms::DialogResult::OK)      
    uc->open(ofd->FileName);        
}

Below code is in UserControl1.xaml.cs

public void open(string path)
{
    MessageBox.Show(path); //path seems to be fine
    img.Source = new BitmapImage(new Uri(path));
}

Below code is in UserControl1.xaml

<UserControl x:Class="WpfControlLibrary1.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid Margin="-54,0,0,0">
        <Image x:Name="img"  Stretch="Uniform" Opacity="1" Source="Koala.jpg"/>
    </Grid>
</UserControl>

tried removing Source image in xaml code, it made no effect

Prakash M
  • 659
  • 1
  • 12
  • 36

1 Answers1

2

The statement

WpfControlLibrary1::UserControl1 ^uc = gcnew UserControl1();

creates a new UserControl1 instance. This is not the instance that you've previously put into the ElementHost, and is therefore not shown in your UI.

Clemens
  • 123,504
  • 12
  • 155
  • 268
  • Hi Clemens, Thanks for your response i tried `WpfControlLibrary1::UserControl1 uc; uc.open(ofd->FileName);` its still same. but it looks like it the same method works in [C#](http://www.codeproject.com/Articles/26311/Integrate-WPF-UserControls-in-WinForms) do you know any other method to call any public function in wpf control from winform? – Prakash M Jul 17 '16 at 17:12
  • That would still create a new instance. As said, you must use the previously ceated instance, i.e. the one added to the ElementHost. – Clemens Jul 17 '16 at 18:20
  • nice. it worked.. but how to work with OOPs. i tried this `WpfControlLibrary1::UserControl1 ^uc;this->elementHost1->Child = nullptr; this->elementHost1->Child = uc; if (ofd->ShowDialog() ==Windows::Forms::DialogResult::OK) uc->open(ofd->FileName);`its throwing error _Object reference not set to an instance of an object._ some silly mistake here i guess! is there a way to remove previous(first) instance and create new dynamically? Thanks – Prakash M Jul 18 '16 at 15:45
  • And what address had uc? nil? – lerthe61 Oct 05 '16 at 12:40