从用户控件单击后无法关闭弹出窗口

时间:2013-02-27 19:46:29

标签: c# windows-8 windows-store-apps

我在使用此代码打开的Popup(DebtPage.xaml中的NewTransaction)中有一个用户控件(AddNewTransaction):

[DebtPage.xaml.cs]

   public void Button_Click_1(object sender, RoutedEventArgs e)
            {
                if (!NewTransaction.IsOpen) { NewTransaction.IsOpen = true; }

                NewTransaction.HorizontalOffset = (Window.Current.Bounds.Width) / 2 - (New_Transaction_Grid.Width / 2);
                NewTransaction.VerticalOffset = (Window.Current.Bounds.Height) / 2 - (New_Transaction_Grid.Height / 2);

                NewTransaction.Visibility = Visibility.Visible;

                RetanguloBranco_Background.Visibility = Visibility.Visible;
            }

Popup打开,并且有一个带有“保存”按钮的自定义控件(AddNewTransaction)。按钮假设保存数据并关闭弹出窗口。但它只是保存,Popup保持打开状态。

[AddNewTransaction.xaml.cs]

private static DebtPage _debtPage;
public static DebtPage debtPage
{
    get
    {
        if (_debtPage == null)
        {
            _debtPage = new DebtPage();
        }
        return _debtPage;
    }
}


private void BotaoSave_Click(object sender, RoutedEventArgs e)
{
    SalvarTransaction(); //It's working!
    debtPage.ClosePopup_NewTransaction(); //It is not!
}

[DebtPage.xaml.cs]

public void ClosePopup_NewTransaction()
        {
            NewTransaction.IsOpen = false;

            NewTransaction.Visibility = Visibility.Collapsed;

            RetanguloBranco_Background.Visibility = Visibility.Collapsed;

        }

谢谢!

1 个答案:

答案 0 :(得分:0)

我用这个解决了这个问题:

private void BotaoSave_Click(object sender, RoutedEventArgs e)
        {
            SalvarTransaction();

            Grid gridParent = this.Parent as Grid;

            Popup parent = gridParent.Parent as Popup;
            if (parent != null)
            {
                parent.IsOpen = false;
            }
        }

但我仍然想知道为什么ClosePopup_NewTransaction();什么都不做。