【问题标题】:(System.ArgumentException) Value does not fall within the expected range exception thrown while open the popup in another one popup in uwp(System.ArgumentException)值不在uwp中的另一个弹出窗口中打开弹出窗口时抛出的预期范围异常
【发布时间】:2020-04-13 15:21:10
【问题描述】:

(System.ArgumentException) 值不在 uwp 中的另一个弹出窗口中打开弹出窗口时抛出的预期范围异常。

单击按钮打开弹出窗口。请参考代码

<Button Width="50" Height="30" Name="btn1" 
            HorizontalAlignment="Center" VerticalAlignment="Center" 
            Content="Click" Click="Button_Click" />
    <Popup Name="popUp1" Width="200" Height="200"/>
    <Popup Name="popUp2" Width="200" Height="200"/>

private void Button_Click(object sender, RoutedEventArgs e)
    {
        Button btn2 = new Button();
        btn2.Width = 200;
        btn2.Height = 50;            
        btn2.Content = "PopUp1";
        popUp1.Child = btn2;
        popUp1.IsOpen = true;
        btn2.Click += Btn2_Click;
    }

    private void Btn2_Click(object sender, RoutedEventArgs e)
    {            
        popUp2.Child = btn1;
        popUp2.IsOpen = true;
    }

【问题讨论】:

    标签: c# uwp-xaml


    【解决方案1】:

    (System.ArgumentException) 值不在预期范围内 抛出范围异常

    此异常是因为您尝试将 btn1 设置为 popUp2 的 Child,但 btn1 已经有父面板。因此,如果您仍然希望 btn1 成为 popUp2 的子级,您可以先将其从其父面板中移除,或者您可以创建一个新 Button 并将其 Click 事件设置为 btn1 的 Click 事件。例如:

    .xaml:

    <StackPanel x:Name="MyPanel">
        <Button Width="50" Height="30" Name="btn1" 
            HorizontalAlignment="Center" VerticalAlignment="Center" 
            Content="Click" Click="Button_Click" />
        <Popup Name="popUp1" Width="200" Height="200"/>
        <Popup Name="popUp2" Width="200" Height="200"/>
    </StackPanel>
    

    .cs:

    private void Btn2_Click(object sender, RoutedEventArgs e)
    {
        MyPanel.Children.Remove(btn1);
        popUp2.Child = btn1;
        popUp2.IsOpen = true;
    }
    

    或者

    private void Btn2_Click(object sender, RoutedEventArgs e)
    {
        Button btn = new Button();
        btn.Width = 200;
        btn.Height = 50;
        btn.Content = "PopUp2";
        btn.Click += Button_Click;
        popUp2.Child = btn;
        popUp2.IsOpen = true;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-12
      • 2015-04-10
      • 2011-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多