WPF图像控制源绑定 [英] WPF Image Control Source Binding
问题描述
我是新来WPF和C#中,我尝试实现以下功能,但很多尝试后失败。谁能帮我?
我有一个图像控制:
<图像Grid.Row =1×:NAME =ImageEditor拉伸=填充StretchDirection =两者/>
我想这个图像控制源绑定到另一个类的静态属性( ImageHandler
)
类ImageHandler
{
公共静态的BitmapImage ImageToDisplay {搞定;组; } 公共ImageHandler(){} .... //其他codeS
}
所以每当我在 ImageHandler
类的东西,并更新 ImageToDisplay
属性,我的形象控制系统将显示新的图像。
我尝试几种方法,但他们没有实现这一目标。下面显示了我失败的尝试之一。
< Window.Resources>
<局部:ImageHandler X:键=ImageHandler>< /地方:ImageHandler>
< /Window.Resources><图像Grid.Row =1×:NAME =ImageEditor拉伸=填充StretchDirection =两者
来源={绑定源= {静态资源的ResourceKey = ImageHandler},
路径= ImageToDisplay,模式=双向}>
< /图像>
您必须实现为 INotifyPropertyChanged的
在 ImageHandler
有关依赖项属性:
类ImageHandler:INotifyPropertyChanged的
{
私人的BitmapImage imageToDisplay;
公众的BitmapImage ImageToDisplay
{
{返回imageToDisplay; }
组
{
如果(imageToDisplay!=值)
{
imageToDisplay =价值;
OnPropertyChanged(ImageToDisplay);
}
}
} 公共ImageHandler(){} // ....其他codeS #区域INotifyPropertyChanged的实施
公共事件PropertyChangedEventHandler的PropertyChanged; 受保护的虚拟无效OnPropertyChanged(字符串propertyName的)
{
PropertyChangedEventHandler处理器=的PropertyChanged;
如果(处理!= NULL)
处理器(这一点,新PropertyChangedEventArgs(propertyName的));
}
#endregion
}
不过,要实现它,我不得不删除静态
属性。
I am new to WPF and C#, I try to implement the following feature, but failed after a lot of attempts. Can anyone help me out?
I have an image control:
<Image Grid.Row="1" x:Name="ImageEditor" Stretch="Fill" StretchDirection="Both"/>
I want to bind the source of this image control to a static property of another class (ImageHandler
)
class ImageHandler
{
public static BitmapImage ImageToDisplay { get; set; }
public ImageHandler(){}
.... //other codes
}
So whenever I do something in the ImageHandler
class, and update the ImageToDisplay
property, my image control will display the new image.
I have try several methods, but none of them achieved this goal. The following shows one of my failed attempts.
<Window.Resources>
<local:ImageHandler x:Key="ImageHandler"></local:ImageHandler>
</Window.Resources>
<Image Grid.Row="1" x:Name="ImageEditor" Stretch="Fill" StretchDirection="Both"
Source="{Binding Source={StaticResource ResourceKey=ImageHandler},
Path=ImageToDisplay,Mode=TwoWay}">
</Image>
You have to implement either INotifyPropertyChanged
in ImageHandler
For dependency property :
class ImageHandler : INotifyPropertyChanged
{
private BitmapImage imageToDisplay;
public BitmapImage ImageToDisplay
{
get { return imageToDisplay; }
set
{
if (imageToDisplay != value)
{
imageToDisplay = value;
OnPropertyChanged("ImageToDisplay");
}
}
}
public ImageHandler() { }
// .... Other codes
#region INotifyPropertyChanged implementation
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
However, to implement it, I had to remove the static
attribute.
这篇关于WPF图像控制源绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!