Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello i made customcontrol,First i created and registered Depencyproperty.
C#
  public static DependencyProperty textproperty; 
textproperty = DependencyProperty.Register("text", typeof(string), typeof(Choosethinkness), new FrameworkPropertyMetadata(new PropertyChangedCallback(changedtext)));      
        public string text
        {
            get { return (string)GetValue(textproperty); }
            set { SetValue(textproperty,value); }
        }
        private static void changedtext(DependencyObject       
        sender,DependencyPropertyChangedEventArgs e)
        {
            
            Choosethinkness ct = (Choosethinkness)sender;
            string oldtext = (string)e.OldValue;
            string newtext = (string)e.NewValue;
            ct.eventraiser(oldtext,newtext);
        }

Then i creted event which will be react on changed text in textbox.
C#
public static readonly RoutedEvent putsthinkness = EventManager.RegisterRoutedEvent("changethinkness",RoutingStrategy.Bubble,typeof(RoutedPropertyChangedEventHandler<string>),typeof(Choosethinkness));
        public event RoutedPropertyChangedEventHandler<string> changethinkness
        {
            add { AddHandler(putsthinkness, value); }
            remove { RemoveHandler(putsthinkness, value); }
        }
 public void eventraiser(string oldtext,string newtext)
        {
            RoutedPropertyChangedEventArgs<string> args = new RoutedPropertyChangedEventArgs<string>(oldtext, newtext);
            args.RoutedEvent = Choosethinkness.putsthinkness;
            RaiseEvent(args);
        }

further i created textbox which will be binded with textproperty.
XML
<TextBox Name="PART_Thinknessdisplay"  RenderTransformOrigin="0,0.5" Canvas.Top="41" Grid.RowSpan="2"   />

At last i overriden OnApplyTemplate() method.
C#
public override void OnApplyTemplate()
      {
          base.OnApplyTemplate();
          TextBox textbox = GetTemplateChild("PART_Thinknessdisplay") as TextBox;

          if (textbox !=null)
          {
              Binding binding = new Binding("text");
              binding.Source = this;
              binding.Mode=BindingMode.OneWayToSource;
              textbox.SetBinding(TextBox.TextProperty,binding);
          }

      }

Customcontrol i implement in next way.
XML
<lib3:Choosethinkness Width="0" Height="20" Canvas.Top="45" Name="Choser" changethinkness="Choser_changethinkness_1"  />

When i enter Text in Textbox there is no reaction.Tell me please what's wrong in my code,or advice me how to implement textchange event in customcontrol.
Posted
Comments
Likefire 3-Sep-15 6:57am    
I guess that a "changethinkness" is not correct name of event. I'm don't see where you calling the "changedtext". To check this out try to define straihgt control of your custom type in code-behind and find accessible event from IntelliSense menu.
Kannan Subramaniam 14-Sep-15 23:37pm    
Try setting the Binding to TwoWay. Also you are not updating the text dependency property on the raised event.
Inimicos 20-Sep-15 14:52pm    
Sorry guys for stupid quastion thank you for advices.I went wrong way from beginning instead i created changedtext event "private void changedtextintextbox(object sender,TextChangedEventArgs e)"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900