2009-06-18 56 views
6

我試圖在WPF中創建一個具有標籤和文本框的可重用UserControl。我想添加屬性到我的UserControl中,以便將兩個子控件的Text字段向上鼓泡,以便於綁定。我讀到我需要通過向DependencyProperties添加所有者來進行一點關注。這是我的代碼現在。它看起來很接近但不完全正確。有任何想法嗎?WPF複合控件

這裏是XAML中:

<UserControl x:Class="MAAD.AircraftExit.Visual.LabelTextBox" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Height="20" Width="300"> 
    <DockPanel> 
     <TextBlock Text="{Binding Path=Label, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" DockPanel.Dock="Left" TextAlignment="Right" Width="122" /> 
     <TextBlock Text=": " DockPanel.Dock="Left"/> 
     <TextBox Text="{Binding Path=Text, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" /> 
    </DockPanel> 
</UserControl> 

而後面的代碼:

public partial class LabelTextBox : UserControl 
{ 
    public static readonly DependencyProperty LabelProperty = DependencyProperty.Register("Label", typeof(string), typeof(LabelTextBox)); 
    public string Label 
    { 
     get { return (string)GetValue(LabelProperty); } 
     set { SetValue(LabelProperty, value); } 
    } 

    public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(LabelTextBox)); 
    public string Text 
    { 
     get { return (string)GetValue(TextProperty); } 
     set { SetValue(LabelTextBox.TextProperty, value); } 
    } 

    public LabelTextBox() 
    { 
     InitializeComponent(); 

     ClearValue(HeightProperty); 
     ClearValue(WidthProperty); 
    } 
} 

編輯:這是最後的工作代碼。我切換到相對源綁定。

+1

我在第一次嘗試同樣的事情,以及假設這是什麼所有者的概念是關於。不幸的是,我遇到了同樣的問題,並最終訴諸於綁定。另外,我的非依賴屬性具有用於傳播屬性更改通知的不同自定義解決方案。 – jpierson 2010-03-23 18:57:51

回答

6

綁定是真的要走的路:

XAML:

<UserControl x:Class="testapp.LabelTextBox " 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
Height="300" Width="300" x:Name="This"> 
<DockPanel> 
    <TextBlock DockPanel.Dock="Left" TextAlignment="Right" Width="70" Name="label" Text="{Binding Label, ElementName=This}" /> 
    <TextBlock Text=": " DockPanel.Dock="Left" /> 
    <TextBox Name="textBox" Text="{Binding Text, ElementName=This}" /> 
</DockPanel> 

代碼背後:

public partial class LabelTextBox : UserControl 
{ 
    public LabelTextBox() 
    { 
     InitializeComponent(); 
     Label = "Label"; 
     Text = "Text"; 
    } 
    public static readonly DependencyProperty LabelProperty = DependencyProperty.Register("Label", typeof(string), typeof(LabelTextBox), new FrameworkPropertyMetadata(LabelPropertyChangedCallback)); 
    private static void LabelPropertyChangedCallback(DependencyObject controlInstance, DependencyPropertyChangedEventArgs args) 
    { 
    } 
    public string Label 
    { 
     get { return (string) GetValue(LabelProperty); } 
     set { SetValue(LabelProperty, value); } 
    } 

    public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(LabelTextBox), new FrameworkPropertyMetadata(TextPropertyChangedCallback)); 
    private static void TextPropertyChangedCallback(DependencyObject controlInstance, DependencyPropertyChangedEventArgs args) 
    { 
    } 
    public string Text 
    { 
     get { return (string) GetValue(TextProperty); } 
     set { SetValue(LabelTextBox.TextProperty, value); } 
    } 
} 
+0

感謝您的代碼,當我嘗試使用它時得到以下輸出: System.Windows.Data錯誤:4:找不到與參考'ElementName = This'綁定的源。 BindingExpression:路徑=標籤;的DataItem = NULL;目標元素是'TextBlock'(Name ='');目標屬性是'文本'(類型'字符串') – 2009-06-18 18:08:04

+0

對不起,我想通了。您將其定義爲控件的名稱。謝謝!! – 2009-06-18 18:21:18

1

我還沒有仔細研究你的實現不起作用的原因,但我不明白你爲什麼這樣做。爲什麼不直接在UserControl上定義所需的依賴項屬性,然後綁定到它們呢?

public static readonly DependencyProperty LabelTextProperty = ...; 

,然後在XAML:

<Label Content="{Binding LabelText}"/>