【问题标题】:Exposing an ImageSource property in a UserControl for use in Blend在 UserControl 中公开 ImageSource 属性以在 Blend 中使用
【发布时间】:2009-01-07 03:47:34
【问题描述】:

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

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

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

edit顺便说一句,我注意到公开 Alignment 或 Margin 属性的行为符合预期,只是图像不起作用。我真的被困在这个问题上,希望能得到帮助!

我当前的代码如下:

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;
    }
}

【问题讨论】:

    标签: wpf user-controls expression-blend dependency-properties


    【解决方案1】:

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

    在我的情况下,我通过将ImageSource 更改为BitmapSource 使其工作。 ImageSource 是抽象的,BitmapSource 是 ImageSource 的扩展。

    但是,对此感觉有些不对劲。 Image.Source 的类型是 ImageSource。不管它是否抽象,似乎我应该能够使用 ImageSource 类型的 DependencyProperty。

    因此,对于我自己的情况,我已经使用 BitmapSource 解决了这个问题,但我仍在调查中。

    编辑:希望您在询问近一年后不介意回答,+/- 12 小时。 ;)

    EDIT2:George,我也确实使用ImageSource 为我工作,使用:

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

    【讨论】:

    • 不,谢谢你的提示!下次我在 Blend 时会试一试。
    • 添加了一个适用于 ImageSource 而不是 BitmapSource 的新编辑。
    猜你喜欢
    • 1970-01-01
    • 2011-01-25
    • 2011-12-26
    • 1970-01-01
    • 2017-07-07
    • 1970-01-01
    • 2012-06-27
    • 1970-01-01
    • 2021-07-25
    相关资源
    最近更新 更多