我希望把我的自定義控件內的圖像,所以我的generic.xaml看上去象下面這樣:如何分配圖片URI在自定義的控制
<Style TargetType="local:generic">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:generic">
<Grid Background="{TemplateBinding Background}">
<Rectangle>
<Rectangle.Fill>
<SolidColorBrush x:Name="BackgroundBrush" Opacity="0" />
</Rectangle.Fill>
</Rectangle>
<TextBlock Text="{TemplateBinding Text}"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Foreground="{TemplateBinding Foreground}"/>
<Image Source="{TemplateBinding Source}"
HorizontalAlignment="Center"
VerticalAlignment="Center"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我隱藏如下:
public class Generic : Control
{
public static DependencyProperty ImageUri = DependencyProperty.Register("Source", typeof(Uri), typeof(generic), new PropertyMetadata(""));
public Uri Source
{
get { return (Uri)GetValue(generic.ImageUri); }
set { SetValue(generic.ImageUri, value); }
}
public generic()
{
this.DefaultStyleKey = typeof(generic);
}
}
Apllication是編譯好的,但同時我想運行它拋出以下異常:
$exception
{System.Windows.Markup.XamlParseException: System.TypeInitializationException:
The type initializer for 'testCstmCntrl.themes.generic' threw an exception. ---> System.ArgumentException: Default value type does not match type of property.
謝謝, Subhen
Thanx先生瓊斯先生,但我試圖從我的XAML分配圖像如下,它不顯示任何圖像: –
Simsons
2010-04-15 12:22:48
另外我不確定爲什麼PropertyMetadata()不能接受URI。 – Simsons 2010-04-15 12:27:48