Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BindingOperations.GetBindingExpression Returning null in WPF

Tags:

c#

binding

wpf

Ive adjusted a bound ViewModel, lets call it MyViewModel to inherit from DependencyObject and switched one of my normal CLR properties, lets call it Name, which used to fire NotifyPropertyChanged() inside the setter, to be a DependencyProperty.

Name is a two-way binding to a TextBox and is working fine.

However, calling BindingOperations.GetBindingExpression(InstantiatedMyViewModel, MyViewModel.NameProperty) always returns null.

1 - Is this because it is not possible to Pass my ViewModel (InstantiatedMyViewModel)in as the first parameter (rather than the instance of the textbox)? I assume that since it's a two-way binding, both the InstantiatedMyViewModeland the TextBox should have some binding knowledge

2 - If it is possible, are there any gotchas im missing?

It's working really well, but when I try to call

like image 783
maxp Avatar asked Feb 21 '26 18:02

maxp


1 Answers

You should use

  var name = InstantiatedMyViewModel.GetValue(MyViewModel.NameProperty)

BindingOperations.GetBindingExpression is used on a control that has a binding to some other object. e.g.

  <TextBox x:Name="textBox1" Text="{Binding Name}" />

Then

  var bindingExpression = BindingOperations.GetBindingExpression(
     textBox1, TextBox.TextProperty);

  // "Name"
  var path = bindingExpression.ParentBinding.Path; 
like image 129
Chui Tey Avatar answered Feb 23 '26 06:02

Chui Tey