自己做了一下,直接传引用,结果不显示。搜了一下,大概知道原因了----同一个控件不能同时位于两个容器内,即传递到子窗口前,要先从父窗体移除。
网上的方案太麻烦,所以,分享下自己的。
父窗体前台:
<Button Content="全屏" Command="{Binding FullScreenCommand}" CommandParameter="{Binding ElementName=chartContainer}"/> <Grid Name="chartContainer"> <Grid Name="chart"> </Grid>
父窗体后台:
FullScreenCommand = new CommandBase( p => { var grid = p as Grid; var form = new FullScreenChartView(); form.ParentGrid = grid; if (grid.Name == "chartRealTimeContainer") { grid.Children.Remove(_realtimeGrid); form.Chart.Children.Add(_realtimeGrid); } else { grid.Children.Remove(_statGrid); form.Chart.Children.Add(_statGrid); } form.Show(); });
因为我是根据不同条件传不同的控件,所以用了if.
子窗体前台:
<Grid> <Grid Grid.Row="1" Name="Chart"/> <Button Content="X" HorizontalAlignment="Right" VerticalAlignment="Top" FontSize="20" Width="40" Height="40" Click="Button_Click"/> </Grid>
子窗体后台:
private void Button_Click(object sender, RoutedEventArgs e) { var grid = Chart.Children[0]; Chart.Children.Remove(grid); ParentGrid.Children.Add(grid); Close(); }
退出全屏后,再把控件恢复回去。