3

I have a checkbox and a comboBox, both constructed at code behind as follows:

 var el = new ComboBox();
 var checkBox = new CheckBox { Name = "myCheckBox" };
 el.SetBinding(UIEelement.IsEnabledProperty, new Binding("IsChecked") { ElementName = checkBox.Name });

They are both placed inside the same Grid.

The binding works fine if I place the Grid directly inside my UserControl. But, when I place the Grid inside a TabItem, then put the TabItem inside a TabControl at my UserControl, the binding doesn't work.

We were able to fix this by changin the binding as follows, but I really want to understand why the previous does not work.

 el.SetBinding(UIEelement.IsEnabledProperty, new Binding("IsChecked") { Source = checkBox });

Is it a problem of scope, even if the checkbox and the combo are in the same one?

ltiveron
  • 579
  • 7
  • 16

2 Answers2

4

Based on Arie's answer I was able to understand the problem.

Apparently my TabItem didn't have a NameScope (NameScope.GetNameScope(tabItem) returns null). So I've set the TabItem namescope as myUserControl namescope:

  NameScope.SetNameScope(tabItem, NameScope.GetNameScope(myUserControl));
ltiveron
  • 579
  • 7
  • 16
3

It's probably an issue concerning namescopes. TabItem is a container and has its own namescope, inwhich you'll have to register the name of the control that you created in codebehind, using myNameScope.RegisterName(), e.g like this. Then you'll be able to bind using ElementName.

Community
  • 1
  • 1
Arie
  • 5,251
  • 2
  • 33
  • 54