wpf继承如何继承自己的自定义控件
我的自定义Picture代码如下
public partial class Picture : UserControl
{
public Image parentImage;
public Picture()
{
InitializeComponent();
DataContext = this;
}
public ImageSource MyImagePath
{
get { return (ImageSource)GetValue(MyImagePathProperty); }
set { SetValue(MyImagePathProperty, value); }
}
// Using a DependencyProperty as the backing store for ImagePath. This enables animation, styling, binding, etc...
public static readonly DependencyProperty MyImagePathProperty =
DependencyProperty.Register("MyImagePath", typeof(ImageSource), typeof(Picture), new UIPropertyMetadata(null));
public string ImagePath
{
get { return (string)GetValue(ImagePathProperty); }
set { SetValue(ImagePathProperty, value); }
}
// Using a DependencyProperty as the backing store for ImagePath. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ImagePathProperty =
DependencyProperty.Register("ImagePath", typeof(string), typeof(Picture), new UIPropertyMetadata(""));
public double Angle
{
get { return (double)GetValue(AngleProperty); }
set { SetValue(AngleProperty, value); }
}
// Using a DependencyProperty as the backing store for Angle. This enables animation, styling, binding, etc...
public static readonly DependencyProperty AngleProperty =
DependencyProperty.Register("Angle", typeof(double), typeof(Picture), new UIPropertyMetadata(0.0));
public double ScaleX
{
get { return (double)GetValue(ScaleXProperty); }
set { SetValue(ScaleXProperty, value); }
}
// Using a DependencyProperty as the backing store for ScaleX. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ScaleXProperty =
DependencyProperty.Register("ScaleX", typeof(double), typeof(Picture), new UIPropertyMetadata(1.0));
public double ScaleY
{
get { return (double)GetValue(ScaleYProperty); }
set { SetValue(ScaleYProperty, value); }
}
// Using a DependencyProperty as the backing store for ScaleY. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ScaleYProperty =
DependencyProperty.Register("ScaleY", typeof(double), typeof(Picture), new UIPropertyMetadata(1.0));
public double X
{
get { return (double)GetValue(XProperty); }
set { SetValue(XProperty, value); }
}
// Using a DependencyProperty as the backing store for X. This enables animation, styling, binding, etc...
public static readonly DependencyProperty XProperty =
DependencyProperty.Register("X", typeof(double), typeof(Picture), new UIPropertyMetadata(0.0));
public double Y
{
get { return (double)GetValue(YProperty); }
set { SetValue(YProperty, value); }
}
// Using a DependencyProperty as the backing store for Y. This enables animation, styling, binding, etc...
public static readonly DependencyProperty YProperty =
DependencyProperty.Register("Y", typeof(double), typeof(Picture), new UIPropertyMetadata(0.0));
}
现在我想再自定义一个控件PUserControl从Picture继承 ,结果报这个错误
错误 1 “TouchExample.PUserControl”的分部声明一定不能指定不同的基类 C:\Users\Administrator.PC-20111029KMQN\Desktop\TouchExample\TouchExample\PUserControl.xaml.cs 20 26 TouchExample
上网查了一下说在设计器中把Usecontrol 改成我引入的Picture
代码如下
<cc:Picture x:Class="TouchExample.PUserControl"
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:cc="clr-namespace:TouchExample"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Image Height="150" HorizontalAlignment="Left" Margin="75,106,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="200" Source="/TouchExample;component/Images/Pic2.jpg" />
</Grid>
</cc:Picture>
结果又报错
错误 3 “TouchExample.Picture”不能是 XAML 文件的根,因为它是使用 XAML 定义的。 行 1 位置 13. C:\Users\Administrator.PC-20111029KMQN\Desktop\TouchExample\TouchExample\PUserControl.xaml 1 13 TouchExample
请问大神们有谁遇到过这种问题。。。。