I'd like to create a custom control based on StackPanel since I need to add some code to the derived class.
I created the custom control in Visual Studio.
When I want to use the custom control in an XAML I get various errors.
I'm new to WPF.
Is it (not) possible to use a customized WPF control just like its base class in XAML (StackPanel in this case) ?
c# code is
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):
<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:
<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'
- Element 'Orientation' not recognized or not accessable
- Property "Orientation" not contained in XML-Namespace "clr-namespace:MyNameSpace". Row ... column ...
- No content can be added to object of type "AbstractForm" (i.e. the
<Image
and another<StackPanel
)