【发布时间】:2011-12-20 15:15:48
【问题描述】:
我正在尝试使用用户控件,其中图像是从其包含元素传入的。目的是让我可以重用一组通用的视觉元素,同时只更改图像。例如:
控件用法:
<DataTemplate DataType={x:Type myType}>
<local:MyControl PlotIconSource="..\Images\Scatter.png"/>
</DataTemplate>
控件内的图片
<UserControl x:Class="MyControl">
<Image Source="{Binding PlotIconSource}"/>
</UserControl>
最后是 MyControl.xaml.cs 代码隐藏中 PlotIconSource 的依赖属性。
public ImageSource PlotIconSource
{
get { return (ImageSource)GetValue(PlotIconSourceProperty); }
set { SetValue(PlotIconSourceProperty, value); }
}
public static readonly DependencyProperty PlotIconSourceProperty =
DependencyProperty.Register(
"PlotIconSource",
typeof(ImageSource),
typeof(PlotHeader),
new UIPropertyMetadata());
我确定我在此过程中遗漏了一些东西,因此我们将不胜感激。
【问题讨论】:
标签: c# wpf xaml user-controls dependency-properties