Click here to Skip to main content
15,885,782 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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:
XML
<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
Updated 20-Apr-11 1:40am
v5
Comments
Venkatesh Mookkan 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 20-Apr-11 6:33am    
Is the TextBox Enabled?
Is it ReadOnly?
Srinivas39 20-Apr-11 6:42am    
It's enabled and not readonly
Venkatesh Mookkan 20-Apr-11 6:58am    
Just post the XAML. It would easy to help you.
Srinivas39 20-Apr-11 7:17am    
please find the code given

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:

C#
[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);
}
 
Share this answer
 
v4
Comments
BobJanova 20-Apr-11 13:20pm    
This I did not know. Good answer, 5.
Sergey Alexandrovich Kryukov 20-Apr-11 18:09pm    
Quite logical, my 5.
--SA
pdsulliv 13-Jun-13 18:14pm    
Backdoor, Nice solution.
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
XML
<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">
                                     <discretebooleankeyframe keytime="00:00:00" value="True" />
                            </booleananimationusingkeyframes>
                        </storyboard>
                    </beginstoryboard>
                </eventtrigger.actions>
            </eventtrigger>

        </textblock.triggers>
    </textblock>
</stackpanel>

    <popup x:name="ConnectPopup" placementtarget="{Binding ElementName=ConnectPopupTrigger}" xmlns:x="#unknown">
                           PopupAnimation="Slide" AllowsTransparency="True" StaysOpen="False">
        <border margin="2 2,0,3" borderthickness="1" borderbrush="DarkSlateGray">
            <!-- The connection section, username, password, connect button -->
            <stackpanel orientation="Vertical" margin="0,0,3,3">
                <stackpanel orientation="Horizontal">
                    <label width="70">User Name:</label>
                    <textbox text="{Binding FXAllUserName}" width="120" margin="1" />

                </stackpanel>
                <stackpanel orientation="Horizontal">
                    <label width="70">Password:</label>
                    <passwordbox x:name="mypassword" width="120" margin="1" />
                </stackpanel>
            </stackpanel>
        </border>
    </popup>

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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900