c# wpf的usercontrol的一个问题

lukang423 2015-11-26 11:26:04
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出来,上面图片,下面文字,请问大神我这里应该怎么改?
...全文
119 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
exception92 2015-11-26
  • 打赏
  • 举报
回复
<Image Grid.Row="0" Source="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type local:ImageButton}},Path=ImgSource}"/> -》一个Image中 指定一个类型为UserControl的控件。思路不对,这样还能叫一个“包含Image的Button"了么,而是一个包含用户控件的Image控件,这样就不是一个Button按钮了,而是一个Image了。 你应该 声明一个类似:

 public class ImageButton : Button
这样的类,然后注册你的ImgSource 属性,在Xaml 中为ImageButton 执行样式,样式中通过构建Image 控件(通过 TemplateBinding 来binding你的注册的Source)来显示图片,通过 ContentPresenter 来显示Button的Content,至于布局可以使用grid 来布局。

110,567

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧