文本框在WPF弹出窗口中不起作用 [英] Text box is not working in WPF Popup

查看:81
本文介绍了文本框在WPF弹出窗口中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在使用WPF弹出窗口,在弹出窗口中,我在那里有一个TextBox,但是在那里找不到任何文本.没有可用的键.

有谁知道是什么问题吗?

您能建议我解决这个问题吗?

更新:

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>



谢谢,
Srinivas



Thanks,
Srinivas

推荐答案

为什么只定义一个网格行和列,为什么要定义网格行和列呢?那是没有意义的.另外,AcceptsReturn属性仅在它是多行文本框时才有用.

最后,您的问题是由于您试图以nt预期的方式使用Popup控件而引起的.表示弹出窗口的hwnd的样式为WS_EX_NOACTIVATE,这意味着弹出窗口无法获得焦点.原因是Popup控件用于工具提示和其他静态显示(如图像),而不用于输入.因此,您应该仔细地重新考虑您的设计意图是否合适.

如果您坚持学习本课程(我不能强烈建议您以其他方式进行操作),则必须使用互操作服务将弹出窗口设置为前景窗口:

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);
}


嗨.

我相信您可以将这些弹出式窗口用作工具提示,这不仅仅是WPF的重点,您可以做任何事情.

除了使该文本框接受击键!.

好的,所以我花了很多时间寻找解决方案,并通过调用
在从托管窗口窗体到WPF包含窗口的键盘事件中看到了很多有关管道的引用.
ElementHost.EnableModelessKeyboardInterop

但是我无法将其与wpf弹出窗口配合使用,我只是无法获得窗口",并且尝试了一些操作.

无论如何,我无意间找到了解决方法.如果您有一个面板,在该面板上有一个弹出窗口,并且在该面板上有一个文本框,然后单击它并键入一些文本,则触发弹出框出现-您的文本框将起作用并被插入.就像基本控件上的文本框触发了所有管道一样.

有点难以解释,但这是代码
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">
                                     <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(对象发送者,RoutedEventArgs e)
{

UIElement element = mytextbox作为UIElement;

if(element!= null)
{
布尔值成功= element.Focus(); //始终返回false,并且不关注焦点.
}

}

我所需要做的只是以编程方式获取隐藏的文本框,以便立即获得焦点,而弹出窗口中的文本框将包含文本.

在没有隐藏的文本框"mytextbox"的情况下尝试使用它,您将发现它不起作用.

好的解决方法很糟糕,但这只是意味着您添加了一个零宽度的文本框和一个快速回调,所有这些都有效.

享受吧.


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.


这篇关于文本框在WPF弹出窗口中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆