我有一个 Silverlight XAML 用户控件,我想用它来显示元素在布局中组合在一起。xml是:
<UserControl x:Class="StylingLibrary.FieldSet"
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:System="clr-namespace:System;assembly=mscorlib"
mc:Ignorable="d"
d:DesignWidth="200" d:DesignHeight="200">
<Grid x:Name="FieldsetLayoutRoot" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Border HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Style="{StaticResource FieldsetBorder}">
<ContentPresenter x:Name="TheFieldsetContentPresenter" Content="{Binding Content}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0"/>
</Border>
<Border HorizontalAlignment="Left" VerticalAlignment="Top" Style="{StaticResource FieldsetTitleBackground}">
<TextBlock x:Name="FieldsetTitleTextBlock" HorizontalAlignment="Left" VerticalAlignment="Top" Text="{Binding Title}" Style="{StaticResource FieldsetTitle}"/>
</Border>
</Grid>
</UserControl>
它的支持代码主要包含依赖属性:
public partial class FieldSet : UserControl
{
public FieldSet()
{
TheFieldsetContentPresenter.DataContext = this;
FieldsetTitleTextBlock.DataContext = this;
// Required to initialize variables
InitializeComponent();
}
public String Title
{
get { return (String)GetValue(TitleProperty); }
set { SetValue(TitleProperty, value); }
}
public static readonly DependencyProperty TitleProperty =
DependencyProperty.Register("TitleProperty", typeof(String), typeof(FieldSet), null);
public new FrameworkElement Content
{
get { return (FrameworkElement)GetValue(MyContentProperty); }
set { SetValue(MyContentProperty, value); }
}
public static readonly DependencyProperty MyContentProperty =
DependencyProperty.Register("ContentProperty", typeof(FrameworkElement), typeof(FieldSet), null);
}
现在每当我尝试像这样使用它时:
<Styling:FieldSet Title="Projects">
<TextBlock Text="test" />
</Styling:FieldSet>
Visual Studio (2010) 告诉我抛出了 NullReferenceException,它无法创建 FieldSet 的实例。尝试构建和运行项目时,出现以下错误:
{System.Windows.Markup.XamlParseException: The invocation of the constructor on type 'StylingLibrary.FieldSet' that matches the specified binding constraints threw an exception. [Line: 81 Position: 44] ---> System.NullReferenceException: Object reference not set to an instance of an object.
at StylingLibrary.FieldSet..ctor()
--- End of inner exception stack trace ---
at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
at ProjectsOverview.Views.ProjectsList.InitializeComponent()
at ProjectsOverview.Views.ProjectsList..ctor(ProjectsListViewModel m)}
我在这里做错了什么?