本文介绍了图片不会改变WPF控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我提到这个,写了几乎相同的代码但是在不同的语言中,它没有按预期工作! 我使用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 xmlns:mc = http://schemas.openxmlformats.org/markup -compatibility / 2006 xmlns:d = http://schemas.microsoft.com/expression/blend/2008 mc:可忽略 = d d:DesignHeight = 300 d:DesignWidth = 300 > < 网格 保证金 = - 54,0,0,0 > < 图片 x:名称 = img 拉伸 = 统一 不透明度 = 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 Eventprivate: 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.cspublic 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控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-22 08:49