【发布时间】:2016-12-30 14:36:13
【问题描述】:
我正在开发一个自定义控件。这个自定义控件有一个像这样的图像:
<Image Source="{TemplateBinding Poster}" />
在我的 C# 文件中,DependencyProperty 如下:
public static readonly DependencyProperty PosterProperty = DependencyProperty.Register(
nameof(Poster),
typeof(ImageSource),
typeof(MovieButton),
new UIPropertyMetadata(null));
public ImageSource Poster
{
get { return (ImageSource)GetValue(PosterProperty); }
set { SetValue(PosterProperty, value); }
}
现在我可以在 XAML 中将以下内容添加到我的用户控件中:Poster="Images/NoPoster.png" 或 Poster = new BitmapImage(new Uri(@"pack://application:,,,/Images/NoPoster.png")) 来自代码。
一切正常。我想知道的是,为什么我不能将Poster 声明为BitmapImage 或string 而不是ImageSource?
【问题讨论】:
标签: c# wpf xaml binding dependency-properties