Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WPF自定义控件设计器看起来不错,但我遇到了运行时问题_Wpf_Templates_Custom Controls_Binding - Fatal编程技术网

WPF自定义控件设计器看起来不错,但我遇到了运行时问题

WPF自定义控件设计器看起来不错,但我遇到了运行时问题,wpf,templates,custom-controls,binding,Wpf,Templates,Custom Controls,Binding,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"

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”。

发现由于缺少初始斜杠/,因此找不到资源

不正确:

<my:XTabItem Header="Tab 1" Image="Resources/icon1.png" />
正确:

<my:XTabItem Header="Tab 1" Image="/Resources/icon1.png" />

发现资源找不到,因为我缺少初始斜杠/。这是正确的路径。