0
votes

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();
}
2
Post code that how are you setting it from code behind?Rohit Vats
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.Tony Vitabile
Thanks for replying... i have edited the code above showing datacontext to be set to my ViewModel class instance.Deepanshu

2 Answers

1
votes

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" />
0
votes

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.