2009-06-18 48 views
6

Tôi đang cố tạo UserControl có thể tái sử dụng trong WPF có Nhãn và Hộp văn bản. Tôi muốn thêm các thuộc tính vào UserControl của mình để bong bóng lên các trường văn bản của cả hai điều khiển con cho đến cha mẹ để dễ ràng buộc. Tôi đọc rằng tôi cần một chút tập trung vào việc lấy nét bằng cách thêm chủ sở hữu vào DependencyProperties. Đây là mã của tôi bây giờ. Nó có vẻ gần nhưng không hoàn toàn đúng. Bất kỳ ý tưởng?Điều khiển tổng hợp WPF

Đây là 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> 

Và mã sau:

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); 
    } 
} 

Edit: Đây là đoạn code làm việc chính thức. Tôi chuyển sang ràng buộc nguồn tương đối.

+1

Tôi đã thử điều tương tự lúc đầu cũng như giả định rằng đó là ý tưởng của chủ sở hữu. Thật không may tôi chạy vào cùng một vấn đề và kết thúc sử dụng để ràng buộc là tốt. Ngoài ra các thuộc tính không phụ thuộc của tôi có một giải pháp tùy chỉnh khác để truyền bá thông báo thay đổi thuộc tính. – jpierson

Trả lời

6

Binding thực sự là con đường để đi:

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> 

Mã Đằng sau:

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

Cảm ơn mã, tôi nhận được kết quả này khi tôi cố gắng sử dụng nó: Lỗi System.Windows.Data: 4: Không thể tìm nguồn để liên kết với tham chiếu 'ElementName = This'. BindingExpression: Đường dẫn = Nhãn; DataItem = null; phần tử đích là 'TextBlock' (Tên = ''); thuộc tính đích là 'Văn bản' (loại 'Chuỗi') –

+0

Xin lỗi, tôi đã tìm ra nó. Bạn đã xác định Đây là tên của điều khiển. Cảm ơn!! –

1

Tôi chưa xem xét chính xác lý do triển khai của bạn không hoạt động, nhưng tôi thực sự không hiểu tại sao bạn làm như vậy. Tại sao không chỉ xác định các thuộc tính phụ thuộc mà bạn cần trên UserControl và sau đó liên kết với chúng?

public static readonly DependencyProperty LabelTextProperty = ...; 

Và sau đó trong XAML của bạn:

<Label Content="{Binding LabelText}"/> 
Các vấn đề liên quan