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
}
}
第9行()的异常:XamlParseException:'在'System.Windows.Baml2006.TypeConverterMarkupExtension'上提供值引发异常。行号“9”和行位置“27”。
答案 0 :(得分:0)
发现资源没有找到,因为我错过了初始斜杠(/)。
不正确:
<my:XTabItem Header="Tab 1" Image="Resources/icon1.png" />
正确:
<my:XTabItem Header="Tab 1" Image="/Resources/icon1.png" />