Image控件的属性扩展

    扩展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等

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
扩展 WPF TreeView 件以自定义图标和可隐藏复选框,你可以通过继承 TreeView 类来实现自定义件。 以下是一个自定义 TreeView 件的示例代码: ```csharp public class CustomTreeView : TreeView { public static readonly DependencyProperty IconProperty = DependencyProperty.Register("Icon", typeof(ImageSource), typeof(CustomTreeView)); public ImageSource Icon { get { return (ImageSource)GetValue(IconProperty); } set { SetValue(IconProperty, value); } } protected override DependencyObject GetContainerForItemOverride() { return new CustomTreeViewItem(); } protected override bool IsItemItsOwnContainerOverride(object item) { return item is CustomTreeViewItem; } } public class CustomTreeViewItem : TreeViewItem { public static readonly DependencyProperty ShowCheckBoxProperty = DependencyProperty.Register("ShowCheckBox", typeof(bool), typeof(CustomTreeViewItem), new PropertyMetadata(true)); public bool ShowCheckBox { get { return (bool)GetValue(ShowCheckBoxProperty); } set { SetValue(ShowCheckBoxProperty, value); } } public static readonly DependencyProperty IconProperty = DependencyProperty.Register("Icon", typeof(ImageSource), typeof(CustomTreeViewItem)); public ImageSource Icon { get { return (ImageSource)GetValue(IconProperty); } set { SetValue(IconProperty, value); } } public override void OnApplyTemplate() { base.OnApplyTemplate(); var checkBox = GetTemplateChild("PART_CheckBox") as CheckBox; if (checkBox != null) { checkBox.Visibility = ShowCheckBox ? Visibility.Visible : Visibility.Collapsed; } var icon = GetTemplateChild("PART_Icon") as Image; if (icon != null) { icon.Source = Icon; icon.Visibility = Icon != null ? Visibility.Visible : Visibility.Collapsed; } } } ``` 在上面的代码中,自定义 TreeView 件包含一个 Icon 属性,该属性可以设置 TreeView 的图标。自定义 TreeViewItem 件包含一个 ShowCheckBox 属性,该属性可以制复选框是否可见,以及一个 Icon 属性,该属性可以设置 TreeViewItem 的图标。在 OnApplyTemplate 方法中,我们可以获取 CheckBox 和 Image 件并设置它们的可见性和图标源。 在 XAML 中使用自定义 TreeView 件可以像这样: ```xml <local:CustomTreeView> <local:CustomTreeViewItem Header="Item 1" Icon="/Images/Icon1.png"> <local:CustomTreeViewItem Header="Subitem 1.1" Icon="/Images/Icon2.png"/> <local:CustomTreeViewItem Header="Subitem 1.2" Icon="/Images/Icon3.png"/> </local:CustomTreeViewItem> <local:CustomTreeViewItem Header="Item 2" Icon="/Images/Icon4.png"> <local:CustomTreeViewItem Header="Subitem 2.1" Icon="/Images/Icon5.png"/> <local:CustomTreeViewItem Header="Subitem 2.2" Icon="/Images/Icon6.png"/> </local:CustomTreeViewItem> </local:CustomTreeView> ``` 在上面的 XAML 代码中,我们可以使用 Icon 属性来设置 TreeView 和 TreeViewItem 的图标,并使用 ShowCheckBox 属性制 TreeViewItem 的复选框是否可见。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值