2010-05-21 49 views
0

MainWindow.xamlWPF自定義控件 - 設計看起來不錯,但我得到一個運行時的問題

<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

它被發現的資源沒有被定位,因爲我錯過了最初的斜槓(/)。 是正確的路徑。 – 2010-05-21 15:20:05

回答

0

發現資源未找到,因爲我缺少初始斜槓(/)。

錯誤:

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

正確:

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