简体   繁体   中英

Wpf - popup issue inside Usercontrol when click on button

I have a Window page, after click a button from window page --> then a UserControl page showing. After Inside UserControl there is a <Popup Name="MyPopup"> popup. The Popup always stays on top problem. How can I solve this issue ?

I have tried,

 <Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525" Background="Green"> <Grid > <Button Height="50" Width="100" Content="window_ClickMe" Click="btnUserManage_Click"></Button> <ContentControl Name="cont2" Visibility="Hidden"> </ContentControl> </Grid> </Window>

and code behind page of window,

private void btnUserManage_Click(object sender, RoutedEventArgs e)
    {
        UC_UserMgmt mw = new UC_UserMgmt();
        cont2.Content = mw;
        cont2.Visibility = Visibility.Visible;
    }

then, this is usercontrol page with popup,

 <UserControl x:Class="WpfApplication1.UC_UserMgmt" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="400" Background="Blue"> <Grid> <Grid Name="g1"> <Button Content="usercontrol_ClickMe" Height="50" Width="150" Margin="150,0,0,250" Click="btnShow_Click"></Button> </Grid> <Popup Name="MyPopup" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" HorizontalOffset="-150" Placement="Mouse" StaysOpen="{Binding ElementName=g1,Path=IsMouseOver}" VerticalOffset="20" AllowsTransparency="True"> <StackPanel> <Border BorderBrush="Black" Background="Brown" BorderThickness="1" Width="300" Height="100" > <Grid> <TextBox x:Name="txtUName" HorizontalAlignment="Center" Height="28" Width="223" TextWrapping="Wrap" VerticalAlignment="Top" Margin="10,26,64.6,0" /> <Button Content="Open" Height="30" Width="50" Margin="238,24,9.6,43.6" Click="btnOpen_Click"/> </Grid> </Border> </StackPanel> </Popup> </Grid> </UserControl>

and this is code behind page of usercontrol,

private void btnOpen_Click(object sender, EventArgs e)
    {
         MyPopup.IsOpen = true;
         System.Windows.Forms.OpenFileDialog fDialog = new System.Windows.Forms.OpenFileDialog();
         fDialog.Title = "Select file to be zip";
         if (fDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
         {
             txtUName.Text = fDialog.FileName.ToString();
         }
    }  

    private void btnShow_Click(object sender, EventArgs e)
    {
        MyPopup.IsOpen = true;
    }

The problem is, when user click on OPEN button, an openFileDialog is opening and when it is opened, the popup seems disappear. How can I solve this problem?

I believe using Popup is not the right choice in your situation. Take a look at Popup.StaysOpen Property , in particular the part that says

When the StaysOpen property is set to true, Popup stays open until it is explicitly closed by setting the IsOpen property to false. When StaysOpen is false, the Popup control intercepts all mouse and keyboard events to determine when one of these events occurs outside the Popup control.

I can't think of a clean way of keeping that popup open when showing the dialog box, because the dialog box is going to create a mouse/keyboard event when you interact with it

But to answer your question, I can think of two choices

1) You can simply move MyPopup.IsOpen = true; in btnOpen_Click to the end of the code block. I assume this will cause the popup to reopen after the dialog box is closed

2) You can create a boolean property or dependency property in your code behind set it to true when showing the popup and bind your Popup's StaysOpen to it, and maybe set it to false in LostFocus for g1 or something like that. Or even set StaysOpen to true and use IsOpen to close your Popup in g1 's LostFocus

Edit - Second Solution How-To Don't use it, dirty code, bad practices, and the popup remains on top of dialogbox

In the Popup set StaysOpen="True"
In every control in the UserControl except the UserControl itself and the textbox set Focusable="False"
Add private bool dont; to the UserControl's code-behind and set it to True in the beginning of btnOpen_Click and set it to False in its end
In the UserControl and the TextBox add LostFocus="UserControl_LostFocus"
And then add this function in UserControl code-behind

private void UserControl_LostFocus(object sender, RoutedEventArgs e)
{
    if(!dont && !txtUName.IsFocused && !IsFocused)
        MyPopup.IsOpen = false;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM