Wpf - 单击按钮时出现usercontrol弹出问题。 [英] Wpf - usercontrol popup problem when click on button.

查看:294
本文介绍了Wpf - 单击按钮时出现usercontrol弹出问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有一个窗口页面,点击窗口页面的按钮后 - >然后显示一个UserControl页面。在Inside UserControl之后,有一个< Popup Name =MyPopup弹出窗口。 Popup始终处于最重要的问题。我怎么能解决这个问题呢?



谢谢,

Abhilash.JA



我尝试过:



我试过了,



先生,这是我的winodow页面,



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,

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





然后,这是带弹出窗口的usercontrol页面,





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>





这是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;
       }





问题是,当用户点击OPEN按钮时,openFileDialog正在打开,当它打开时打开后,弹出窗口似乎消失了。我怎么解决这个问题。请帮助我......



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...

推荐答案

如果你看一下你的代码,那么一旦对话框打开它就会关闭,因为它失去了焦点。



我宁愿使用其他东西然后弹出窗口,但这里有两个解决方案,每个都有它自己的缺点,因此最好使用另一种方法。



1:将用户控件代码更改为这样,保留其余部分。

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



您会看到这会导致另一个问题。





2:用这个替换btnopen_click


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



这将是最佳解决方案,但弹出窗口的起始位置在第二个IsOpen上有所不同。你应该能够解决这个问题。


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.


这篇关于Wpf - 单击按钮时出现usercontrol弹出问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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