c# wpf的usercontrol的一个问题
ImageButton.xaml里:
<UserControl x:Class="用户控件.ImageButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:用户控件"
mc:Ignorable="d"
d:DesignHeight="60" d:DesignWidth="50">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="3*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Image Grid.Row="0" Source="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type local:ImageButton}},Path=ImgSource}"/>
<TextBlock Grid.Row="1" Text="{Binding Content,RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type local:ImageButton}}}"/>
</Grid>
</UserControl>
ImageButton.xaml.cs里:
namespace 用户控件
{
/// <summary>
/// ImageButton.xaml 的交互逻辑
/// </summary>
public partial class ImageButton : UserControl
{
public ImageButton()
{
InitializeComponent();
}
public ImageSource ImgSource
{
get { return (ImageSource)GetValue(ImgSourceProperty); }
set { SetValue(ImgSourceProperty, value); }
}
public static readonly DependencyProperty ImgSourceProperty = DependencyProperty.Register("ImgSource", typeof(ImageSource), typeof(ImageButton), new PropertyMetadata("Image",new PropertyChangedCallback(OnImgSourceChanged)));
static void OnImgSourceChanged(Object sender, DependencyPropertyChangedEventArgs args)
{
ImageButton source = (ImageButton)sender;
source.ImgSource = (ImageSource)args.NewValue;
}
}
}
MainWindow.xaml里
<Window x:Class="用户控件.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:用户控件"
Title="MainWindow" Height="350" Width="525">
<Grid>
<local:ImageButton ImgSource="/Resources/summer.jpg" Content="asuna"/>
</Grid>
</Window>
但是调试的时候就发生了 System.Windows.Markup.XamlParseException
HResult=-2146233087
Message=“用户控件.ImageButton”的类型初始值设定项引发异常。
Source=PresentationFramework
LineNumber=7
LinePosition=10
StackTrace:
在 System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
在 System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri)
在 System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
在 System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
在 用户控件.MainWindow.InitializeComponent() 位置 c:\Users\Dell\AppData\Local\Temporary Projects\用户控件\MainWindow.xaml:行号 1
在 用户控件.MainWindow..ctor() 位置 C:\Users\Dell\AppData\Local\Temporary Projects\用户控件\MainWindow.xaml.cs:行号 24
InnerException: System.ArgumentException
HResult=-2147024809
Message=默认值类型与属性“ImgSource”类型不匹配。
Source=WindowsBase
StackTrace:
在 System.Windows.DependencyProperty.ValidateDefaultValueCommon(Object defaultValue, Type propertyType, String propertyName, ValidateValueCallback validateValueCallback, Boolean checkThreadAffinity)
在 System.Windows.DependencyProperty.RegisterCommon(String name, Type propertyType, Type ownerType, PropertyMetadata defaultMetadata, ValidateValueCallback validateValueCallback)
在 System.Windows.DependencyProperty.Register(String name, Type propertyType, Type ownerType, PropertyMetadata typeMetadata, ValidateValueCallback validateValueCallback)
在 System.Windows.DependencyProperty.Register(String name, Type propertyType, Type ownerType, PropertyMetadata typeMetadata)
在 用户控件.ImageButton..cctor() 位置 C:\Users\Dell\AppData\Local\Temporary Projects\用户控件\ImageButton.xaml.cs:行号 34
InnerException:
我就是想做一个ImageButton出来,上面图片,下面文字,请问大神我这里应该怎么改?