我有一個簡單的用戶控件Silverlight的用戶控件綁定
的XAML:
<UserControl x:Class="GraemeGorman_Controls.Navigation.NavigationItem"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Border x:Name="borderLayoutRoot">
<TextBlock x:Name="textBlockCaption" Text="{Binding Caption}" />
</Border>
</UserControl>
CS:
namespace GraemeGorman_Controls.Navigation
{
public partial class NavigationItem : UserControl
{
public static readonly DependencyProperty CaptionProperty = DependencyProperty.Register(
"Caption",
typeof (string),
typeof (NavigationItem),
new PropertyMetadata(new PropertyChangedCallback(OnCaptionChanged)));
public string Caption
{
get {return (string)GetValue(CaptionProperty);}
set {SetValue(CaptionProperty, value);}
}
public NavigationItem()
{
InitializeComponent();
}
private static void OnCaptionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
//null
}
}
}
什麼我的問題是,當我的標題從未創建控件的一個實例顯示 - 現在我已經使用e.NewValue測試了OnCaptionChanged函數中的屬性,並且它是正確的值。我的綁定有什麼問題?
如果我寫在後面caption屬性的代碼集
textBlockCaption.Text = value;
看起來很好...
任何幫助表示讚賞
感謝 格雷姆
嗨 這就是它!謝謝! – 2009-11-26 13:34:28
如果修改DataContext,則無法使用高一級的數據綁定(使用UserControl的形式) – Grigory 2012-07-26 12:38:09