![](/img/trans.png)
[英]System.ArgumentException: 'Value does not fall within the expected range.'
[英](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 中的另一個彈出窗口中打開彈出窗口時引發的預期范圍異常。
從按鈕單擊打開彈出窗口。 請參考代碼
<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;
}
(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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.