绑定到BitmapImage [英] Binding to BitmapImage
本文介绍了绑定到BitmapImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
{
public BitmapImage testimage {get;组; }
Thumbnail()
{
testimage = new BitmapImage(new Uri(http://www.diseno-art.com/news_content/wp-content/uploads/2012/09/ 2013-Jaguar-F-Type-1.jpg));
}
}
自定义控件中的图像元素: p>
< Image x:Name =previewImagex:FieldModifier =publicMargin =8HorizontalAlignment =StretchVerticalAlignment =StretchSource ={Binding Path = thumbnail.testimage}Stretch =Fill/>
当我创建一个控件时,我这样做:
MyControl MC = new MyControl();
MC.DataContext = new Thumbnail();
图像不显示 - 为什么?
解决方案
XAML:
x:Name =previewImagex:FieldModifier =publicMargin =8HorizontalAlignment =StretchVerticalAlignment =StretchSource ={Binding Path = testimage}Stretch =Fill/>
代码(更清洁):
public class Thumbnail:INotifyPropertyChanged
{
private BitmapImage tmpbmp;
public BitmapImage testimage {get {return tmpbmp; } set {tmpbmp = value; OnPropertyChanged(testimage); }}
public Thumbnail()
{
tmpbmp = new BitmapImage(new Uri(http://www.diseno-art.com/news_content/wp-content/uploads/2012/ 09/2013-Jaguar-F-Type-1.jpg));
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if(handler!= null)
handler(this,new PropertyChangedEventArgs(propertyName));
}
}
public class Thumbnail : INotifyPropertyChanged
{
public BitmapImage testimage { get; set; }
Thumbnail()
{
testimage = new BitmapImage(new Uri("http://www.diseno-art.com/news_content/wp-content/uploads/2012/09/2013-Jaguar-F-Type-1.jpg"));
}
}
And a image element in a custom control:
<Image x:Name="previewImage" x:FieldModifier="public" Margin="8" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Source="{Binding Path=thumbnail.testimage}" Stretch="Fill" />
When i create a control, i do this:
MyControl MC = new MyControl();
MC.DataContext = new Thumbnail();
And image doesn't show up - why?
解决方案
XAML:
<Image x:Name="previewImage" x:FieldModifier="public" Margin="8" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Source="{Binding Path=testimage}" Stretch="Fill" />
code (cleaner):
public class Thumbnail : INotifyPropertyChanged
{
private BitmapImage tmpbmp;
public BitmapImage testimage { get { return tmpbmp; } set { tmpbmp = value; OnPropertyChanged("testimage"); } }
public Thumbnail()
{
tmpbmp = new BitmapImage(new Uri("http://www.diseno-art.com/news_content/wp-content/uploads/2012/09/2013-Jaguar-F-Type-1.jpg"));
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
这篇关于绑定到BitmapImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文