Xaml 绑定到UserControl DependencyProperty无法按预期工作

Xaml 绑定到UserControl DependencyProperty无法按预期工作,xaml,windows-phone-8,data-binding,user-controls,Xaml,Windows Phone 8,Data Binding,User Controls,我的Windows Phone 8 Silverlight应用程序的每个页面都有一个标题。为了停止重复我自己,我决定创建一个简单的UserControl witch,它包含一个网格和一个文本块。TextBlock的文本绑定到UserControl的DependencyProperty(标题属性)。请参阅末尾的代码 为了使到TitleProperty的绑定正常工作,我需要使用以下代码将UserControl的DataContext绑定到自身:DataContext=“{binding Relati

我的Windows Phone 8 Silverlight应用程序的每个页面都有一个标题。为了停止重复我自己,我决定创建一个简单的UserControl witch,它包含一个网格和一个文本块。TextBlock的文本绑定到UserControl的DependencyProperty(标题属性)。请参阅末尾的代码

为了使到TitleProperty的绑定正常工作,我需要使用以下代码将UserControl的DataContext绑定到自身:
DataContext=“{binding RelativeSource={RelativeSource Mode=Self}}”

当在页面中使用控件并为Title属性设置固定值静态绑定时,此功能正常工作。例:

这项工作:


您是否尝试过在代码隐藏中设置UserControl的DataContext?(您将删除xaml中的setter)

编辑:尝试在xaml中设置控件的设计实例。(同时保留代码隐藏语句)


是的,我有。这样做会破坏Title属性.No上的UserControl绑定。这没有帮助。d:DesignInstance适用于设计时数据。我的问题不是在设计时。对不起,我误解了这个问题。这是一个似乎更接近你所经历的问题。
<uiuc:SubSectionHeader Title="My fixed title" />
<uiuc:SubSectionHeader Title="{Binding Source={StaticResource Literals}, Path=Literals.applicationSettings, FallbackValue='[Application Settings]'}" />
<uiuc:SubSectionHeader Title="{Binding Path=UserName'}" />
<UserControl x:Class="UI.UserControls.SubSectionHeader"
             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:converters="clr-namespace:UI.Converters"
             mc:Ignorable="d"
             FontFamily="{StaticResource PhoneFontFamilyNormal}"
             FontSize="{StaticResource PhoneFontSizeNormal}"
             Foreground="{StaticResource PhoneForegroundBrush}"
             d:DesignHeight="50"
             d:DesignWidth="480"
             DataContext="{Binding RelativeSource={RelativeSource Mode=Self}}">

    <UserControl.Resources>
        <converters:StringCaseConverter x:Name="ToUppercase" />
        <converters:BooleanToVisibilityConverter x:Key="BoolToVisibility" />
    </UserControl.Resources>

    <Grid x:Name="LayoutRoot"
          Height="50"
          Background="{Binding Source={StaticResource ThemeSetter}, Path=BackgroundColor}">

        <TextBlock VerticalAlignment="Center"
                   Margin="25 0 0 0"
                   Style="{StaticResource PhoneTextNormalStyle}"
                   Foreground="White"
                   Text="{Binding Path=Title, Mode=TwoWay, Converter={StaticResource ToUppercase}, ConverterParameter='upper', FallbackValue='[SUB SECTION TITLE]'}" />

    </Grid>
</UserControl>
using System.Windows;
using System.Windows.Controls;

namespace UI.UserControls {

    public partial class SubSectionHeader : UserControl {
        public SubSectionHeader() {
            InitializeComponent();
        }

        public string Title {
            get { return (string)GetValue( TitleProperty ); }
            set { SetValue( TitleProperty, value ); }
        }

        public static readonly DependencyProperty TitleProperty = DependencyProperty.Register(
            "Title",
            typeof( string ),
            typeof( SubSectionHeader ),
            new PropertyMetadata( "[SubSection Title]" )
        );

    }
}
  public partial class SubSectionHeader : UserControl {
        public SubSectionHeader() {
            InitializeComponent();
            DataContext = this;
        }
<UserControl
    x:Class="MyControl
    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"
    mc:Ignorable="d"
    d:DesignHeight="300"
    d:DesignWidth="400"
    d:DataContext="{d:DesignInstance}">