0

I have created a custom control CustomTextBox inherited from TextBox class. I have created a dependency property named CustomTextProperty.

I have binded this DP with my Viewmodel property.

While Registering the DP i have given the property change callback but it is only get called one time when my control gets the binded data initially when my xaml loads.

When i try to set my control from view the binded VM property setter does not gets called and also the propertychangecallback not gets fired.

Please help!!

Code snipet below:

My Custom control

class CustomTextBox : TextBox
{
  public static readonly DependencyProperty CustomTextProperty = DependencyProperty.Register("CustomText",
                                                               typeof(string), typeof(CustomTextBox),
                                                               new FrameworkPropertyMetadata("CustomTextBox",
                                                               FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
                                                               new PropertyChangedCallback(OnCustomPropertyChange)));

 public string CustomText
 {
   get { return (string)GetValue(CustomTextProperty); }
   set { SetValue(CustomTextProperty, value); }
 }

 private static void OnCustomPropertyChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
 {
   // This is Demo Application.
   // Code to be done Later...
 }
}

My View Model:

public class ViewModel : INotifyPropertyChanged
{
 private string textForTextBox;

 public string TextForCustomTextBox
 {
   get
   {
     return this.textForTextBox;
   }
   set
   {
     this.textForTextBox = value;

     this.OnPropertyChange("TextForCustomTextBox");
   }
 }

 public event PropertyChangedEventHandler PropertyChanged;

 public void OnPropertyChange(string name)
 {
   PropertyChangedEventHandler handler = PropertyChanged;

   if (handler != null)
   {
     handler(this, new PropertyChangedEventArgs(name));
   }
 }
}

My Xaml Code with Binding:

<custom:CustomTextBox x:Name="CustomTextBox" 
                                  CustomText="{Binding TextForCustomTextBox, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                  Grid.Row="1" HorizontalAlignment="Center" Width="200" Height="50" />

My Code Behind to set DataContext:

// My View Constructor
public View1()
{
  InitializeComponent();

  this.DataContext = new ViewModel();
}
3
  • Post code that how are you setting it from code behind?
    – Rohit Vats
    Sep 3, 2014 at 17:10
  • Where's the code that sets the DataContext? Did you set a DataContext somewhere in the XAML or in the code-behind? Everything you've posted looks like it will work. Sep 3, 2014 at 17:14
  • Thanks for replying... i have edited the code above showing datacontext to be set to my ViewModel class instance.
    – Deepanshu
    Sep 3, 2014 at 17:23

2 Answers 2

1

You said that you declared a CustomText DependencyProperty and data bound it to your view model TextForCustomTextBox property and that much is correct. However, when you said that you tried to set your property from the view, you were mistaken.

What you were actually doing was setting the CustomTextBox .Text property from the view and that wasn't connected to your CustomTextBox.CustomText property. You can connect them like this, although I'm not quite sure what the point of that would be:

<Views:CustomTextBox x:Name="CustomTextBox" Text="{Binding CustomText, RelativeSource={
    RelativeSource Self}, UpdateSourceTrigger=PropertyChanged}" CustomText="{Binding 
    TextForCustomTextBox, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
    Grid.Row="1" HorizontalAlignment="Center" Width="200" Height="50" />
1
  • Thanks Sheridan... your provided solution made it work. Actually there is no point of doing so, but i m learning WPF and want to learn every in and out of this technology. Can you suggest me any good documentation or handbook for learning WPF.
    – Deepanshu
    Sep 4, 2014 at 8:53
0

Try setting your DataContext BEFORE the actual initialization so it is available when the form/control objects are created. If it can't find before, is that what may be causing the failed bindings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.