提供的 DependencyObject 不是此 Freezable WPF c# 的上下文 [英] The provided DependencyObject is not a context for this Freezable WPF c#
本文介绍了提供的 DependencyObject 不是此 Freezable WPF c# 的上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试用 OpenFileDialog
中的图像填充 Reactangle
.
I am trying to fill a Reactangle
with an image from OpenFileDialog
.
XAML
<Rectangle RadiusX="15" RadiusY="15">
<Rectangle.Fill>
<ImageBrush x:Name="userpic" RenderOptions.BitmapScalingMode="Fant" ImageSource="/icons/usericon.png"/>
</Rectangle.Fill>
</Rectangle>
C#
private void LoadImg() {
System.Windows.Forms.OpenFileDialog ofpd = new System.Windows.Forms.OpenFileDialog();
ofpd.Filter = "Image Files(*.BMP;*.JPG;*.PNG;*.JPEG)|*.BMP;*.JPG;*.PNG;*.JPEG";
ofpd.Title = "Add a photo";
if ((ofpd.ShowDialog == System.Windows.Forms.DialogResult.OK)) {
userpic.ImageSource = new BitmapImage(new Uri(ofpd.FileName));
}
}
但是它抛出了一个错误:提供的 DependencyObject 不是这个 Freezable 的上下文.
..知道为什么会发生这种情况吗?
But it's throwing an error : The provided DependencyObject is not a context for this Freezable.
..Any idea on why this is happening ?
我已经阅读了这个但是也许我无法应用该解决方案,因为我不是从代码隐藏中创建 ImageBrush
...??
I already read this but maybe i cant apply the solution as i am not creating the ImageBrush
from code-Behind...??
推荐答案
为矩形指定名称
<Rectangle x:Name="rect" RadiusX="15" RadiusY="15"/>
然后创建一个新的ImageBrush并将其分配给Rectangle的Fill属性:
Then create a new ImageBrush and assign it to the Rectangle's Fill property:
var fill = new ImageBrush(new BitmapImage(new Uri(ofpd.FileName)));
RenderOptions.SetBitmapScalingMode(fill, BitmapScalingMode.Fant);
rect.Fill = fill;
这篇关于提供的 DependencyObject 不是此 Freezable WPF c# 的上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文