WPF Custom Control - Designer looks fine, but I get a runtime issue...
- by myermian
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:my="clr-namespace:MyStuff;assembly=MyStuff"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TabControl Margin="5">
<TabItem Header="Start Page" />
<my:XTabItem Header="Tab 1" Image="Resources/icon1.png" />
</TabControl>
</Grid>
</Window>
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:MyStuff"
>
<!-- XTabItem -->
<Style TargetType="{x:Type local:XTabItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:XTabItem}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<StackPanel Orientation="Horizontal">
<Image Source="{Binding Path=Image, RelativeSource={RelativeSource TemplatedParent}}"
Stretch="UniformToFill" MaxHeight="24" />
<TextBlock Text="{TemplateBinding Header}" />
<Button Content="X" />
</StackPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
XTabItem.cs
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace MyStuff
{
public class XTabItem : TabItem
{
#region Dependency Properties
public static readonly DependencyProperty ImageProperty;
#endregion
#region Constructors / Initializer
static XTabItem()
{
//Initialize the control as "lookless".
DefaultStyleKeyProperty.OverrideMetadata(typeof(XTabItem), new FrameworkPropertyMetadata(typeof(XTabItem)));
//Setup the dependency properties.
ImageProperty = DependencyProperty.Register("Image", typeof(ImageSource), typeof(XTabItem), new UIPropertyMetadata(null));
}
#endregion
#region Custom Control Properties (Image)
/// <summary>
/// The image (icon) displayed by inside the tab header.
/// </summary>
/// <remarks>The image is specified in XAML as an absolute or relative path.</remarks>
[Description("The image displayed by the button"), Category("Optional Properties")]
public ImageSource Image
{
get { return (ImageSource)GetValue(ImageProperty); }
set { SetValue(ImageProperty, value); }
}
#endregion
}
}
Exception at line #9 () : XamlParseException : 'Provide value on 'System.Windows.Baml2006.TypeConverterMarkupExtension' threw an exception.' Line number '9' and line position '27'.