在WPF中创建Adorner控件

时间:2014-08-04 13:00:45

标签: wpf vb.net adorner

我在WPF中使用VB.Net 2012

代码

Public Class UserControl1
Inherits Adorner

Sub New(AdornedElement As UIElement)
    MyBase.New(AdornedElement)
    'Err Found Here
End Sub

Private Sub Button_Click(sender As Object, e As RoutedEventArgs)

End Sub
End Class

错误:基类' System.Windows.Controls.UserControl'为类' UserControl1'指定不能与基类不同的是System.Windows.Documents.Adorner'其中一种部分类型。

请帮助

。 。 。 。 编辑:添加了用户控件的XAML代码

<UserControl x:Class="UserControl1"
         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" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>


    <TextBlock Text=" This is Child Control " HorizontalAlignment="Center" VerticalAlignment="Center"/>
    <Button Grid.Row="1" Content="Close Child Control " HorizontalAlignment="Center" VerticalAlignment="Center" Click="Button_Click"/>
</Grid>
</UserControl>

编辑:Errs。添加将UserControl更改为Adorner

Error   6   Base class 'System.Windows.Controls.UserControl' specified for class 'UserControl1' cannot be different from the base class 'System.Windows.Documents.Adorner' of one of its other partial types.   D:\Data - 2012\Temp\WPF Adorners\WPF Adorners\obj\Debug\UserControl1.g.i.vb 42  14  WPF Adorners
Error   1   Cannot add content to an object of type "Adorner".  D:\Data - 2012\Temp\WPF Adorners\WPF Adorners\UserControl1.xaml 8   5   WPF Adorners
Error   2   Type 'Adorner' is not usable as an object element because it is not public or does not define a public parameterless constructor or a type converter.   D:\Data - 2012\Temp\WPF Adorners\WPF Adorners\UserControl1.xaml 1   2   WPF Adorners
Error   3   The type 'Adorner' does not support direct content. D:\Data - 2012\Temp\WPF Adorners\WPF Adorners\UserControl1.xaml 8   5   WPF Adorners
Error   4   Cannot add content to object of type 'System.Windows.Documents.Adorner'. Line 8 Position 6. D:\Data - 2012\Temp\WPF Adorners\WPF Adorners\UserControl1.xaml 8   6   WPF Adorners

2 个答案:

答案 0 :(得分:2)

正如@Robert建议的那样。

当您更改相应XAML文件后面的代码中的继承时,您必须更改XAML中的标记。

您需要在XAML中更改如下:(请参阅第一个标记和最后一个标记)

<Adorner x:Class="UserControl1"
         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" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>


    <TextBlock Text=" This is Child Control " HorizontalAlignment="Center" VerticalAlignment="Center"/>
    <Button Grid.Row="1" Content="Close Child Control " HorizontalAlignment="Center" VerticalAlignment="Center" Click="Button_Click"/>
</Grid>
</Adorner>

更新

Adorner是一个抽象类,因此您不能直接使用它,而是必须在此之外创建派生类,然后从本地Adorner类派生UserControl或使用任何现有的派生的Adorner类。

查看有关此主题的this代码项目文章。

答案 1 :(得分:1)

查看XAML以获取控件 - 它会说基类是UserControl。将其更改为Adorner以与VB代码保持一致。