1
votes

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

1

1 Answers

2
votes

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.