Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: .NET3.5 C# WPF
Hello Guys,
 
I'm working with WPF Popup, in the popup, I have a TextBox there but I couldn't hit any text there. No keys are working for that.
 
Does any one know what the problem is?
 
Can you please suggest me solution to this problem?
 
Update:
<Popup Name="popup1">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="40"></RowDefinition>
                    <RowDefinition Height="40"></RowDefinition>
                </Grid.RowDefinitions>
                <Grid Grid.Row="0">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="100"></ColumnDefinition>
                    </Grid.ColumnDefinitions>
                    <TextBox Name="desc" Grid.Column="0" AcceptsReturn="True" IsReadOnly="False" MaxLength="500" IsEnabled="True" Focusable="True"></TextBox>
                </Grid>
            <Grid Grid.Row="1">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="100"></ColumnDefinition>
                    </Grid.ColumnDefinitions>
                    <ComboBox Grid.Column="0" 
                       <ComboBoxItem>Item1</ComboBoxItem> 
                    </ComboBox>
                <Grid>
            </Grid>
        </Popup>
 
Thanks,
Srinivas
Posted 20-Apr-11 0:03am
Edited 20-Apr-11 1:40am
v5
Comments
Venkatesh Mookkan at 20-Apr-11 6:05am
   
Can you post the XAML (if necessary codebehind) so that it is easy for us to figure out the problem?
Henry Minute at 20-Apr-11 6:33am
   
Is the TextBox Enabled?
Is it ReadOnly?
Srinivas39 at 20-Apr-11 6:42am
   
It's enabled and not readonly
Venkatesh Mookkan at 20-Apr-11 6:58am
   
Just post the XAML. It would easy to help you.
Srinivas39 at 20-Apr-11 7:17am
   
please find the code given
Prerak Patel at 20-Apr-11 6:56am
   
Don't you like to share code?
Srinivas39 at 20-Apr-11 7:08am
   
<Popup Name="popup1"><GRID><GRID.ROWDEFINITIONS> <ROWDEFINITION height="40"></ROWDEFINITION></GRID.ROWDEFINITIONS><GRID grid.row="0"><GRID.COLUMNDEFINITIONS> <COLUMNDEFINITION width="100"></COLUMNDEFINITION></GRID.COLUMNDEFINITIONS><TEXTBOX name="desc" grid.column="0" acceptsreturn="True" isreadonly="False" maxlength="500" isenabled="True" focusable="True"> </TEXTBOX></GRID></GRID></Popup>
Srinivas39 at 20-Apr-11 7:13am
   
please find the code above, this is from an XAML file
BobJanova at 20-Apr-11 7:38am
   
Are you giving the textbox focus? Is there some control which is hogging the focus, or overlaying the text box so you can't select it?
Srinivas39 at 20-Apr-11 7:42am
   
I made focusable is true too, But when i keep my mouse over the textbox and select, it's not been selecting and no focus :(
Srinivas39 at 20-Apr-11 7:43am
   
nothing on the popup, is selectable including labels... is there any issue with popup?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Why are you defining grid rows and columns when you only have one of each? That's just pointless. Also, the AcceptsReturn property is only useful if it's a multi-line text box.
 
Finally, your problem is caused by the fact that you're trying to use the Popup control in a way that is nt intended. The hwnd that represents the popup has the style WS_EX_NOACTIVATE, meaning the popup can't gain the focus. The reason for that is that the Popup control is used for tooltips and other static displays (like images), NOT for input. You should therefore carefully reconsider whether or not your design intentions are even appropriate.
 
If you INSIST on pursuing this course (and I can't recommend strongly enough that you do it a different way), you have to use interop services to set the popup window as the foreground window:
 
[DllImport("USER32.DLL")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetForegroundWindow(IntPtr hWnd);
 
public static void ActivatePopup(Popup popup)
{
    //try to get a handle on the popup itself (via its child)
    HwndSource source = (HwndSource)PresentationSource.FromVisual(popup.Child);
    IntPtr handle = source.Handle;
 
    //activate the popup
    SetForegroundWindow(handle);
}
  Permalink  
v4
Comments
BobJanova at 20-Apr-11 13:20pm
   
This I did not know. Good answer, 5.
SAKryukov at 20-Apr-11 18:09pm
   
Quite logical, my 5.
--SA
pdsulliv at 13-Jun-13 18:14pm
   
Backdoor, Nice solution.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hi.
 
I believe you can use such popups for more than just tooltips, that's the whole point of WPF, you can do anything.
 
Except get the darn textboxes to accept keystrokes !.
 
Okay so I trawled around a fair bit to find a solution and saw lots of references to Plumbing in the keyboard events from the hosting windows form to your WPF contained window by calling
 
ElementHost.EnableModelessKeyboardInterop
 
But I couldn't get this to work with a wpf popup, I just couldn't get the "Window" and I tried a few things.
 
Anyway I accidentally found a work around. If you have a panel from which you have a popup, and you have a textbox on that panel, and you click into it and type in some text, then you trigger your popup to come up - youre textbox will work and be plumbed in. Its as if the text box on teh base control triggers all the plumbing.
 
Kind of hard to explain, but here's the code
        <stackpanel background="LightSeaGreen" orientation="Horizontal">
 
            <textbox x:name="mytextbox" width="0" xmlns:x="#unknown" />
            <textblock x:name="blessme" text="This is the popup button." mouseenter="popupUC_Loaded" xmlns:x="#unknown">
                <textblock.triggers>
                    <eventtrigger routedevent="Mouse.MouseUp">
                        <eventtrigger.actions>
                            <beginstoryboard>
                                <storyboard>
                                    <booleananimationusingkeyframes storyboard.targetname="ConnectPopup">
                                                                    Storyboard.TargetProperty="IsOpen">
void popupUC_Loaded(object sender, RoutedEventArgs e)
{
 
UIElement element = mytextbox as UIElement;
 
if (element != null)
{
Boolean success = element.Focus(); //Always returns false and doesn't take focus.
}
 
}
 
All I had to do programtically was get the hidden text box to gain focuss for an instant and the textbox in your popup would contain text.
 
Try it without the hidden text box "mytextbox" and you'll see it doesn't work.
 
Ok a poor work around, but just means you add a zero width text box and a quick callback and it all works.
 
Enjoy.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 6,656
1 Sergey Alexandrovich Kryukov 6,400
2 CPallini 5,240
3 George Jonsson 3,584
4 Gihan Liyanage 2,625


Advertise | Privacy | Mobile
Web04 | 2.8.140921.1 | Last Updated 1 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100