我正在开发 WPF 软件。在主窗口中,我有一个名为“gridWindow”的网格和一些按钮。每个按钮点击事件实际上会分别调出不同的窗口,然后抓取窗口中的内容添加到主窗口的网格中。
private void btnWelcome_Click(object sender, RoutedEventArgs e)
{
Window_Welcome childWindow = new Window_Welcome();
object PrjWindowContent = childWindow.Content;
childWindow.Content = null;
gridWindow.Children.Add(childWindow as UIElement);
}
然后我有另一个按钮需要打开一个名为“BlackScreen”的新窗口并镜像主窗口网格中的所有内容。MainWindow 中网格内的内容始终根据系统时间或来自其他 MainWindow 控件的用户输入而变化。镜像窗口的内容也需要相应地改变。
我了解到我可能需要一个视觉画笔来复制屏幕。但我就是没有这样做。
这是我在 BlackScreen.xaml 中的代码。
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid Grid.Row="0" Background="Black">
<Viewbox Name="vbox" Stretch="Uniform"><!--DataContext="{Binding ElementName=gridWindow}"-->
<Grid>
<Grid.Background>
<VisualBrush Stretch="Uniform" Visual="{Binding}">
</VisualBrush>
</Grid.Background>
</Grid>
</Viewbox>
</Grid>
</Grid>
这是一个简单的示例,说明如何使用 VisualBrush
来“镜像”您的主要内容 Grid
(或您的 UI 的任何其他部分):
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid Name="MainContent">
<Ellipse Fill="Red" />
<Rectangle Fill="Yellow" Margin="50" />
</Grid>
<Rectangle Grid.Row="1">
<Rectangle.Fill>
<VisualBrush Visual="{Binding ElementName=MainContent}" />
</Rectangle.Fill>
</Rectangle>
</Grid>
这会产生以下内容:
我是一名优秀的程序员,十分优秀!