图片不会改变WPF控件 [英] Picture not changing in WPF control
问题描述
我提到这个,写了几乎相同的代码但是在不同的语言中,它没有按预期工作!
我使用winform(C ++ / Cli)作为主机& 'WPF用户控件Libaray'(C#)作为子控件。
使用Winform中的ElementHost组件在winforms中集成WPF用户控件(PictureBox)。
基本上我想改变WPF控件中的图片来自winform按钮。它编译和编译运行正常。
但唯一的问题是,即使图片的路径正确,图像也不会改变。
代码以下是winform button_click事件
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->文件名);
}
以下代码位于UserControl1.xaml.cs
public void open( string path)
{
MessageBox.Show(path); // 路径似乎很好
img.Source = new BitmapImage( new Uri(path));
}
以下代码位于UserControl1.xaml
< UserControl x:类 = WpfControlLibrary1.UserControl1
xmlns = http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x = http://schemas.microsoft .com / winfx / 2006 / xaml
< span class =code-attribute> xmlns:mc = http://schemas.openxmlformats.org/markup -compatibility / 2006
xmlns:d = http://schemas.microsoft.com/expression/blend/2008
< span class =code-attribute> mc:可忽略 = d
d:DesignHeight = 300 d:DesignWidth = 300 >
< 网格 保证金 = - 54,0,0,0 >
< 图片 x:名称 = img 拉伸 < span class =code-keyword> = 统一 不透明度 = 1 来源 = 考拉。 jpg / >
< / Grid >
< / UserControl >
我尝试了什么:
尝试删除xaml代码中的源图像,它没有效果
图像的加载不是那么简单。看看这个文档或其他示例代码
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>
What I have tried:
tried removing Source image in xaml code, it made no effect
The loading of an image isnt so short handed. Take a look at this documentation or other example code.
这篇关于图片不会改变WPF控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!