代码之家  ›  专栏  ›  技术社区  ›  George Sealy

在UserControl中公开ImageSource属性以在Blend中使用

  •  0
  • George Sealy  · 技术社区  · 16 年前

    我有一个用户控件,它公开了ImageSource类型的属性。我想在Blend中公开此属性,以便在Blend中编辑它,而不是在代码中指定图像。

    根据我在谷歌上搜索到的内容,我添加了一个依赖属性,并指定了适当的属性以在Blend中公开该属性。

    我可以在那里看到它,并编辑它(作为文本字段)。我想做的是有一个可用图像资源的下拉列表,以及一个用于加载另一个图像的浏览按钮。换句话说,我希望它的行为类似于“Image”控件的“Source”属性。

    编辑

    public static readonly DependencyProperty ImageSourceProperty =
            DependencyProperty.Register("ImageSource", typeof(ImageSource), typeof(SmartButton));
    
    [Category("Appearance")]
    [Bindable(true)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public ImageSource ImageSource
    {
        get { return (ImageSource)GetValue(ImageSourceProperty); }
        set
        {
            SetValue(ImageSourceProperty, value);
            this.BackgroundImage.Source = value;
        }
    }
    
    1 回复  |  直到 15 年前
        1
  •  1
  •   JMD    15 年前

    我一直在研究这个问题(减去Blend,再加上在XAML的ControlTemplate中使用属性)

    在我的情况下,我通过改变 ImageSource BitmapSource

    然而,有些事情让人感觉不对劲。图像的类型。源为ImageSource。无论它是否抽象,似乎我应该能够使用ImageSource类型的DependencyProperty。

    所以,就我自己的情况来说,我已经让它工作了 位图源

    编辑:希望你不介意在你问了差不多一年后的回答,+/-12小时

    EDIT2:乔治,我也确实让这个为我工作 使用:

    public static readonly DependencyProperty SourceProperty =
        Image.SourceProperty.AddOwner(typeof(MyCustomButton));
    public ImageSource Source
    {
        get { return (ImageSource)GetValue(SourceProperty); }
        set { SetValue(SourceProperty, value); }
    }