【问题标题】:How to close a ChildWindow from an UserControl button loaded inside it?如何从其中加载的 UserControl 按钮关闭 ChildWindow?
【发布时间】:2010-12-20 16:20:08
【问题描述】:

这是我的 ChildWindow xaml 代码:

1    <Grid x:Name="LayoutRoot">
2       <Grid x:Name="teste">
3       <Grid.ColumnDefinitions>
4        <ColumnDefinition Width="*"/>
5        <ColumnDefinition Width="*"/>
6       </Grid.ColumnDefinitions>
7       <Grid.RowDefinitions>
8        <RowDefinition />
9        <RowDefinition Height="Auto" />
10      </Grid.RowDefinitions>
11      <local:UserControl1 Grid.Row="0" Grid.ColumnSpan="2"/>
12     </Grid>
13   </Grid>

这是我的 UserControl1 xaml 代码:

1     <Grid x:Name="LayoutRoot" Background="#FFA34444">
2      <Button Click="Child_Close" Content="Cancel">
3     </Grid>

这是我的 UserControl C#:

private void Child_Close(object sender, System.Windows.RoutedEventArgs e)
{
 ChildWindow cw = (ChildWindow)this.Parent;
 cw.Close();
}

尝试这种方式是行不通的。 有什么想法吗?

Tks 乔西

【问题讨论】:

    标签: c# silverlight childwindow


    【解决方案1】:

    这是我当前的(临时)解决方案 - 一个 ChildPopupWindowHelper 静态类来打开弹出窗口,而无需为我想要公开的每个用户控件创建一个愚蠢的 ChildWindow XAML 实例。

    • 照常创建用户控件,继承自ChildWindowUserControl

    • 然后用

      打开一个弹出窗口

      ChildPopupWindowHelper.ShowChildWindow("TITLE", new EditFooControl())

    我对此并不完全满意,欢迎对此模式进行改进。


    public static class ChildPopupWindowHelper
    {
        public static void ShowChildWindow(string title, ChildWindowUserControl userControl)
        {
            ChildWindow cw = new ChildWindow()
            {
                Title = title
            };
            cw.Content = userControl;
            cw.Show();
        }
    }
    
    public class ChildWindowUserControl : UserControl
    {
        public void ClosePopup()
        {
            DependencyObject current = this;
            while (current != null)
            {
                if (current is ChildWindow)
                {
                    (current as ChildWindow).Close();
                }
    
                current = VisualTreeHelper.GetParent(current);
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      UserControl 的父级的问题不是ChildWindow,而是子窗口内的 Grid。您需要获取UserControl 的父级的父级才能导航到ChildWindow:-

      ChildWindow cw = (ChildWindow)((FrameworkElement)this.Parent).Parent;
      

      但是,将其嵌入您的 UserControl 是不好的做法,您需要向 UserControl 的消费者规定它可以放置在哪里。在上述情况下,要使用户控件正常工作,它必须始终是 Layout 根的直接子级。

      更好的方法是在可视化树中搜索ChildWindow。我会使用这个辅助方法(实际上我会将它放在辅助扩展静态类中,但我会在这里保持简单)。

      private IEnumerable<DependencyObject> Ancestors()
      {
          DependencyObject current = VisualTreeHelper.GetParent(this);
          while (current != null)
          {
              yield return current;
              current = VisualTreeHelper.GetParent(current);
          }
      }
      

      现在您可以使用 LINQ 方法来获取 ChildWindow:-

      ChildWindow cw = Ancestors().OfType<ChildWindow>().FirstOrDefault();
      

      这将找到您的 UserControl 的第一个祖先恰好是 ChildWindow。这允许您将 UserControl 放置在子窗口 XAML 中的任何深度,它仍然会找到正确的对象。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-14
        相关资源
        最近更新 更多