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)
Here is my first C# attempt at this. Something is still not quite right. Nothing happens so I'm not quite sure where my mistake is.
StackPanel sp = new StackPanel();
TextBox tb = new TextBox();
Popup popup = new Popup();
TextBox popupTextBox = new TextBox();
popup.Child = popupTextBox;
sp.Children.Add(tb);
sp.Children.Add(popup);
this.Content = sp;
//***Questions concerning giving the UIElement a name and registering it
tb.Name = "siblingTextBox";
System.Windows.NameScope.GetNameScope(tb).RegisterName("siblingTextBox", tb);
//***Questions concerning giving the UIElement a name and registering it
popupTextBox.Name = "popupTextBox";
System.Windows.NameScope.GetNameScope(tb).RegisterName("popupTextBox", popupTextBox);
Binding binding = new Binding();
binding.ElementName = tb.Name;
popup.PlacementTarget = tb;
Style style = new Style();
style.TargetType = typeof(Popup);
DataTrigger dataTrigger = new DataTrigger();
Binding focusedBinding = new Binding("IsFocused");
focusedBinding.ElementName = tb.Name;
dataTrigger.Value = true;
dataTrigger.Binding = focusedBinding;
Setter setter = new Setter();
setter.Property = Popup.IsOpenProperty;
setter.Value = true;
dataTrigger.Setters.Add(setter);
style.Triggers.Add(dataTrigger);
dataTrigger = new DataTrigger();
focusedBinding = new Binding("IsFocused");
focusedBinding.ElementName = popupTextBox.Name;
dataTrigger.Value = true;
dataTrigger.Binding = focusedBinding;
setter = new Setter();
setter.Property = Popup.IsOpenProperty;
setter.Value = true;
dataTrigger.Setters.Add(setter);
style.Triggers.Add(dataTrigger);
popup.Style = style;