0

要素がレイアウト内でグループ化されていることを示すために使用したい Silverlight XAML ユーザー コントロールがあります。xaml は次のとおりです。

<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

バインディングを使用するのではなく、CustomControl を作成して TemplateBinding を使用する必要があります。その後、このコントロールを別の場所から使用できるようになり、問題を解決します。

Silverlight でのテンプレート バインディングとカスタム コントロール

乾杯!ヴィノード

于 2012-07-12T10:42:50.657 に答える
0

Change the constructor to

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

First you need initialize the components, then you can set the DataContext to them.

于 2012-07-12T08:08:43.153 に答える