我想设计一种按钮,在这个按钮里有一个Source属性,设置后就可显示图片,
ImageButton.cs
public partial class ImageButton:Button
{
public ImageSource ImgSource
{
get { return (ImageSource)GetValue(ImgSourceProperty); }
set { SetValue(ImgSourceProperty, value); }
}
public static readonly DependencyProperty ImgSourceProperty = DependencyProperty.Register("ImgSource", typeof(ImageSource), typeof(ImageButton), null);
}
app.xaml里
<ControlTemplate x:Key="template1" TargetType="{x:Type local:ImageButton}">
<Grid Name="grid10" Width="50" Height="70">
<Grid.RowDefinitions>
<RowDefinition Height="4*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Image Grid.Row="0" Source="{TemplateBinding ImgSource}"/>
<TextBlock Grid.Row="1" Text="{TemplateBinding Content}"/>
</Grid>
</ControlTemplate>
然而这样做,在MainWindow.xaml里
<local:ImageButton x:Name="image" Content="按钮1" Template="{StaticResource template1}" ImgSource="Resources/pic1.png"/>
调试的时候没有错,但是不会显示图片,请问大神要怎样做才能显示图片?
不知道你这个问题是否已经解决, 如果还没有解决的话: