I have a TextBox and a Popup control. I want the popup IsOpen property to be bound to the TextBox IsFocused property. In other words, if the textbox has focus, the popup is open. Alternatively, if the popup is in focus, I don't want it to close due to the textbox losing focus. I was hoping to handle this using bindings rather than having to deal with this in event handlers. Also, do I have to do anything regarding the dependency properties, since they are pre-existing(i.e. Register, override metadata, etc), or can I just bind to these properties.
Here is some sample code similar to my scenario
StackPanel sp = new StackPanel();
TextBox tb = new TextBox();
Popup popup = new Popup();
sp.Children.Add(tb);
sp.Children.Add(popup);
this.Content = sp;
Binding bd = new Binding("IsFocused");
bd.source = tb.IsFocused;
popup.SetBinding(Popup.IsOpenProperty, bd);
From this I was assuming that if I clicked on the textbox control and gave it focus, that the popup would open, and conversely if the textbox lost focus, that the popup would close. I can't seem to get this to work.
If someone has an idea of what I'm doing wrong, then maybe they could also answer the second half of my question that if the textbox loses focus but it was the popup that receives focus, that the popup will remain open or give focus back to the textbox so that it will remain open bc of the first binding. Any other control that gains focus when the textbox loses focus does not apply to this scenario.
If I could reword this for clarity I would say it like this. 1.) Bind Popup.IsOpen to TextBox.IsFocused 2.) Bind TextBox.IsFocused to Popup.IsFocused(assuming this will just give focus back to the textbox)