简体   繁体   English

是否可以像在XAML中使用其基类(在这种情况下为StackPanel)一样使用自定义WPF控件?

[英]Is it (not) possible to use a customized WPF control just like its base class (StackPanel in this case) in XAML?

I'd like to create a custom control based on StackPanel since I need to add some code to the derived class. 我想基于StackPanel创建自定义控件,因为我需要向派生类添加一些代码。

I created the custom control in Visual Studio. 我在Visual Studio中创建了自定义控件。

When I want to use the custom control in an XAML I get various errors. 当我想在XAML中使用自定义控件时,出现各种错误。

I'm new to WPF. 我是WPF的新手。

Is it (not) possible to use a customized WPF control just like its base class in XAML (StackPanel in this case) ? 是否可以像在XAML(在这种情况下为StackPanel)中的基类一样使用自定义WPF控件?

c# code is C#代码是

namespace MyNameSpace
{
    public class AbstractForm : StackPanel
    {
        static AbstractForm()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(AbstractForm), new FrameworkPropertyMetadata(typeof(AbstractForm)));
        }
        protected override Size MeasureOverride(Size constraint)
        {
            return base.MeasureOverride(constraint);
        }
        protected override Size ArrangeOverride(Size arrangeBounds)
        {
            return base.ArrangeOverride(arrangeBounds);
        }
        // more code
    }
}

Generic.xaml content is (most parts generated when creating the custom control): Generic.xaml的内容是(创建自定义控件时生成的大多数部分):

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:MyNameSpace">

    <Style TargetType="{x:Type local:AbstractForm}"
        BasedOn = "{StaticResource {x:Type StackPanel}}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:AbstractForm}">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

I then try to use "AbstractForm" like a StackPanel in MainWindow.xaml: 然后,我尝试在MainWindow.xaml中像StackPanel一样使用“ AbstractForm”:

<Window x:Name="MyApp" x:Class="MyNameSpace.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:control="clr-namespace:MyNameSpace"
        mc:Ignorable="d"
        Title="MyApp" BorderBrush="{DynamicResource {x:Static SystemColors.ActiveCaptionTextBrushKey}}">
    <StackPanel x:Name="MyAppUI" Orientation = "vertical" HorizontalAlignment="Left" Margin="0,0,0,0" VerticalAlignment="Top">
        <control:AbstractForm x:Name="HeaderArea" Orientation = "horizontal" HorizontalAlignment="Left" Margin="0,0,0,0" VerticalAlignment="Top">
            <Image x:Name="appImage" HorizontalAlignment="Left" VerticalAlignment="Top" Source = "AppImage.gif"/>
            <StackPanel Orientation = "vertical" HorizontalAlignment="Left" Margin="0,0,0,0" VerticalAlignment="Top">
                <!-- more content -->
            </StackPanel>
        </control:AbstractForm>
        <!-- more content -->
    </StackPanel>
</Window>

When running my application I see the following errors (trying to re-translate from german): 运行我的应用程序时,我看到以下错误(试图从德语重新翻译):

  • The 'Orientation' property was not found in 'AbstractForm' 在“ AbstractForm”中找不到“ Orientation”属性
  • Element 'Orientation' not recognized or not accessable 无法识别或无法访问元素“方向”
  • Property "Orientation" not contained in XML-Namespace "clr-namespace:MyNameSpace". XML命名空间“ clr-namespace:MyNameSpace”中不包含属性“ Orientation”。 Row ... column ... 行列 ...
  • No content can be added to object of type "AbstractForm" (ie the <Image and another <StackPanel ) 不能将任何内容添加到“ AbstractForm”类型的对象(即<Image和另一个<StackPanel

A StackPanel is not derived from Control and has therefore no Template property. StackPanel不是从Control派生的,因此没有Template属性。 Hence the ResourceDictionary does not compile. 因此,ResourceDictionary无法编译。

Remove the Setter from the default Style, then rebuild your project. 从默认样式中删除设置器,然后重建项目。

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:MyNameSpace">

    <Style TargetType="{x:Type local:AbstractForm}"
           BasedOn="{StaticResource {x:Type StackPanel}}">
    </Style>
</ResourceDictionary>

It is also questionable if you need a default Style for your AbstractForm class at all. 如果您的AbstractForm类根本不需要默认的样式,这也是一个问题。

If not, just delete the entire Generic.xaml file and the static constructor that overrides the default value of the DefaultStyleKey property. 如果不是,则只需删除整个Generic.xaml文件和覆盖DefaultStyleKey属性的默认值的静态构造函数。 In other words, simply create a class derived from StackPanel. 换句话说,只需创建一个从StackPanel派生的类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM