Click here to Skip to main content
15,885,900 members
Please Sign up or sign in to vote.
0.00/5 (No 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
C++
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
C#
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
XML
<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>


What I have tried:

tried removing Source image in xaml code, it made no effect
Posted
Updated 13-Jan-17 0:17am

1 solution

The loading of an image isnt so short handed. Take a look at this documentation or other example code.
 
Share this answer
 
Comments
Prakash1206 14-Jan-17 6:53am    
Thanks for your answer & helpful links. this question has been answered here
we should not create a new instance of WpfControl

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900