自定义控件.依赖项属性同步更新内部控件属性

 1 using System.Windows;
 2 using System.Windows.Controls;
 3 using System.Windows.Data;
 4 
 5 namespace CXTWpfControlLibrary
 6 {
 7     /// <summary>
 8     /// DControl.xaml 的交互逻辑
 9     /// </summary>
10     public partial class DControl : UserControl
11     {
12         public DControl()
13         {
14             InitializeComponent();
15             Binding binding = new Binding();
16             // Set source object
17             binding.Source = this;
18             // Set source property
19             binding.Path = new PropertyPath("Text");
20             binding.Mode = BindingMode.TwoWay;
21             binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
22             // Attach to target property
23             TB1.SetBinding(TextBox.TextProperty, binding);
24             LB1.SetBinding(Label.ContentProperty, binding);
25             LB2.SetBinding(Label.ContentProperty, binding);
26         }
27 
28         /// <summary>
29         /// Identifies the Value dependency property.
30         /// </summary>
31         public static readonly DependencyProperty TextProperty =
32             DependencyProperty.Register(
33                 "Text", typeof(string), typeof(DControl),
34                 new FrameworkPropertyMetadata(
35                     "",
36                     new PropertyChangedCallback(OnTextChanged)));
37 
38         /// <summary>
39         /// Gets or sets the value assigned to the control.
40         /// </summary>
41         public string Text
42         {
43             get { return (string)GetValue(TextProperty); }
44             set { SetValue(TextProperty, value); }
45         }
46 
47         private static object CoerceValue(DependencyObject element, object value)
48         {
49             string newText = (string)value;
50             DControl control = (DControl)element;
51 
52             //newText = newText;
53 
54             return newText;
55         }
56 
57         private static void OnTextChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
58         {
59             DControl control = (DControl)obj;
60 
61             RoutedPropertyChangedEventArgs<string> e = new RoutedPropertyChangedEventArgs<string>(
62                 (string)args.OldValue, (string)args.NewValue, TextChangedEvent);
63             control.OnTextChanged(e);
64         }
65 
66 
67 
68 
69 
70         /// <summary>
71         /// Identifies the ValueChanged routed event.
72         /// </summary>
73         public static readonly RoutedEvent TextChangedEvent = EventManager.RegisterRoutedEvent(
74             "ValueChanged", RoutingStrategy.Bubble,
75             typeof(RoutedPropertyChangedEventHandler<string>), typeof(DControl));
76 
77         /// <summary>
78         /// Occurs when the Value property changes.
79         /// </summary>
80         public event RoutedPropertyChangedEventHandler<string> TextChanged
81         {
82             add { AddHandler(TextChangedEvent, value); }
83             remove { RemoveHandler(TextChangedEvent, value); }
84         }
85 
86         /// <summary>
87         /// Raises the ValueChanged event.
88         /// </summary>
89         /// <param name="args">Arguments associated with the ValueChanged event.</param>
90         protected virtual void OnTextChanged(RoutedPropertyChangedEventArgs<string> args)
91         {
92             RaiseEvent(args);
93         }
94     }
95 }
<UserControl x:Class="CXTWpfControlLibrary.DControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d"
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <StackPanel>
            <TextBox x:Name="TB1"/>
            <Label x:Name="LB1"/>
            <Label x:Name="LB2"/>
        </StackPanel>
    </Grid>
</UserControl>

 

posted on 2013-03-16 12:01  xiaoboz  阅读(330)  评论(0)    收藏  举报

导航