Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The following code demonstrates having two text boxes in a StackPanel, setting focus to the top text box will open the Popup. At which point, if you then move Focus to the text box contained in the Popup it will remain open. If you move focus to another element, in this instance the second text box in the StackPanel, the Popup will close. As you unable to focus the Popup itself I am actually binding to the IsFocused property of the text box in the Popup.</p> <pre><code>&lt;StackPanel&gt; &lt;TextBox x:Name="text" Text="This is a text box" /&gt; &lt;TextBox Text="Another Text Box" /&gt; &lt;Popup PlacementTarget="{Binding ElementName=text}"&gt; &lt;Popup.Style&gt; &lt;Style TargetType="{x:Type Popup}"&gt; &lt;Style.Triggers&gt; &lt;DataTrigger Binding="{Binding ElementName=text, Path=IsFocused}" Value="True"&gt; &lt;Setter Property="IsOpen" Value="True" /&gt; &lt;/DataTrigger&gt; &lt;DataTrigger Binding="{Binding ElementName=popupText, Path=IsFocused}" Value="True"&gt; &lt;Setter Property="IsOpen" Value="True" /&gt; &lt;/DataTrigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; &lt;/Popup.Style&gt; &lt;TextBox x:Name="popupText" Text="HELLO WORLD" /&gt; &lt;/Popup&gt; &lt;/StackPanel&gt; </code></pre> <p>To achieve the same thing in C# you don't have to use the ElementName bindings as you already have the elements right at hand. I almost always use XAML to define my elements so I'm sure you could tidy this a bit.</p> <pre><code>var text1 = new TextBox { Name = "text", Text = "This is a text box" }; var text2 = new TextBox { Text = "Another Text Box" }; var popupText = new TextBox { Name = "popupText", Text = "HELLO WORLD" }; var popup = new Popup { Child = popupText, PlacementTarget = text1 }; var stackPanel = new StackPanel(); stackPanel.Children.Add(text1); stackPanel.Children.Add(text2); stackPanel.Children.Add(popup); var popupStyle = new Style(typeof (Popup)); var textIsFocusedTrigger = new DataTrigger { Binding = new Binding {Source = text1, Path = new PropertyPath("IsFocused")}, Value = true }; textIsFocusedTrigger.Setters.Add(new Setter(Popup.IsOpenProperty, true)); var popupTextIsFocusedTrigger = new DataTrigger { Binding = new Binding {Source = popupText, Path = new PropertyPath("IsFocused")}, Value = true }; popupTextIsFocusedTrigger.Setters.Add(new Setter(Popup.IsOpenProperty, true)); popupStyle.Triggers.Add(textIsFocusedTrigger); popupStyle.Triggers.Add(popupTextIsFocusedTrigger); popup.Style = popupStyle; </code></pre> <p>I hope this helps!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload