0

我有一个 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)}

我在这里做错了什么?

4

2 回答 2

0

将构造函数更改为

public FieldSet()
{
    InitializeComponent();
    TheFieldsetContentPresenter.DataContext = this;
    FieldsetTitleTextBlock.DataContext = this;
}

首先你需要初始化组件,然后你可以设置DataContext它们。

于 2012-07-12T08:08:43.153 回答
0

您需要创建 CustomControl 并使用 TemplateBinding 而不是使用 Binding。然后您将能够从其他地方使用此控件,我将解决您的问题。

Silverlight 中的模板绑定和自定义控件

干杯! 维诺德

于 2012-07-12T10:42:50.657 回答