Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,

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 ?

Thanks,
Abhilash.J.A

What I have tried:

I have tried,

Sir, this is my winodow page,

HTML
<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,

C#
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,

HTML
<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,

C#
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. Please help me...
Posted
Updated 20-Jan-17 8:54am

1 solution

If you have a look at your code it makes sense that it will close once the dialog opens because it loses focus.

I would rather use something else then popups, but here is two solutions each with it's own drawbacks, and for that reason it's better to use another approach.

1: Change usercontrols code to look like this, keep the rest of it.
public UC_UserMgmt()
{
    InitializeComponent();
    MyPopup.StaysOpen = true;
}

As you will see this causes another problem by itself.


2: Replace btnopen_click with this
System.Windows.Forms.OpenFileDialog fDialog = new System.Windows.Forms.OpenFileDialog();
        fDialog.Title = "Select file to be zip";
        MyPopup.IsOpen = false;
        if (fDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            txtUName.Text = fDialog.FileName.ToString();
            MyPopup.IsOpen = true;
        }

This would be the best solution but the start location of the popup differs on the second IsOpen. You should be able to fix that.
 
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