文本框在WPF弹出窗口中不起作用

文本框在WPF弹出窗口中不起作用

本文介绍了文本框在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

推荐答案

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


<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弹出窗口中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 13:32