Silverlight 如何修复这个简单的样式化Usercontrol,使其依赖于标题和内容?

Silverlight 如何修复这个简单的样式化Usercontrol,使其依赖于标题和内容?,silverlight,xaml,user-controls,dependency-properties,Silverlight,Xaml,User Controls,Dependency Properties,我有一个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.micr

我有一个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>
现在每当我尝试这样使用它时:

<Styling:FieldSet Title="Projects">
       <TextBlock Text="test" />
</Styling:FieldSet>

我在这里做错了什么?

将构造函数更改为

public字段集()
{
初始化组件();
TheFieldsetContentPresenter.DataContext=this;
FieldsetTitleTextBlock.DataContext=this;
}

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

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

干杯!
Vinod

好的,这很有帮助,现在它实际渲染了!但在做了Zabavsky建议的工作之后,它提出了一个新问题(见上文)。下一个问题是,我猜在这个usercontrol上设置内容的datacontext会使其他控件无法将其datacontext放在其中。当我在其中放置itemscontrol时,这些项不再设置。有办法解决这个问题吗?(或者我应该为此创建一个新线程?)
<Styling:FieldSet Title="Projects">
       <TextBlock Text="test" />
</Styling: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)}