扩展Image的属性,显示Web下的图片。
对Image的属性进行扩展:
using System; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Ink; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using System.Windows.Media.Imaging; namespace Longshine.SLLib.LSControlPropertyExtend { /// <summary> /// 描述:用来显示web下的图片 /// </summary> public class ImageSource { private Image images; public ImageSource(Image _img) { images = _img; } public static void SetUrlSource(DependencyObject dependencyObject, string value) { dependencyObject.SetValue(UrlSourceProperty, value); } public static string GetUrlSource(DependencyObject dependencyObject) { return (string)dependencyObject.GetValue(UrlSourceProperty); } // Using a DependencyProperty as the backing store for UrlSource. This enables animation, styling, binding, etc... public static readonly DependencyProperty UrlSourceProperty = DependencyProperty.RegisterAttached("UrlSource", typeof(string), typeof(ImageSource), new PropertyMetadata("", new PropertyChangedCallback(SetSource))); private static void SetSource(DependencyObject o, DependencyPropertyChangedEventArgs e) { Image images = o as Image; if (images != null) { Uri u = new Uri(Application.Current.Host.Source, e.NewValue.ToString()); BitmapImage imgsrc = new BitmapImage(u); images.Source = imgsrc; } } } }
扩展属性的使用:
xmlns:ext="clr-namespace:Longshine.SLLib.LSControlPropertyExtend;assembly=Longshine.SLLib"
<Image ext:ImageSource.UrlSource="../img/printer.png" Stretch="None" ></Image>
注意: printer.png图片在Web工程的ClientBin文件的img文件中
扩展属性的延伸:
这种属性扩展方法都可以应用到任何Silverlight控件中,包括菜单、GridView等