我可以将XAML / WPF窗口复制到第二个窗口,如画中画电视吗?

时间:2012-08-02 16:54:17

标签: c# .net wpf xaml

我有一个带有两个XAML / WPF窗口(从NavigationWindow派生)的应用程序,每个窗口包含一个父UserControl,其中放置了所有子控件。在其中一个窗口中,我想以像画中画电视的方式显示第二个窗口的内容(实际上只是父UserControl)。通过这种方式,用户可以查看第一个窗口,并同时查看第二个窗口中发生的情况。请注意,我不想要第二个窗口的UserControl的两个独立副本(这很容易),而是在第一个窗口中镜像第二个窗口的内容。

这与Windows 7任务栏缩略图预览模糊地相似,所以我认为它必须是可行的。然而,理想情况下,我还希望能够与窗口中的窗口进行交互,就像我拉起原始窗口一样。

这与this question类似,不同之处在于我只想从同一个应用程序中复制一个窗口,而不是整个桌面。也类似于this question,但我需要更多的手握,因为我不太熟悉C#/ WPF。一些代码片段会很棒。

提前谢谢!

2 个答案:

答案 0 :(得分:3)

使用可视笔刷。它不会是互动的,但这似乎符合您的需求。

将此代码粘贴到Kaxaml以查看其实际效果。

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Page.Triggers>
        <EventTrigger RoutedEvent="Page.Loaded">
            <EventTrigger.Actions>
                <BeginStoryboard>
                    <Storyboard Storyboard.TargetName="sampleAnimation" Storyboard.TargetProperty="(FrameworkElement.RenderTransform).(RotateTransform.Angle)">
                        <DoubleAnimation Duration="00:00:10" RepeatBehavior="Forever" To="-360">
                            <DoubleAnimation.EasingFunction>
                                <ElasticEase/>
                            </DoubleAnimation.EasingFunction>
                        </DoubleAnimation>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger.Actions>
        </EventTrigger>
    </Page.Triggers>
    <Page.Resources>
        <VisualBrush x:Key="contentBrush" Stretch="None" Visual="{Binding ElementName=content}"/>
    </Page.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <TextBlock FontSize="25" Text="Content to mirror:"/>
        <Grid
            x:Name="content"
            Grid.Row="1"
            Margin="5"
            Background="#11000000"
            ClipToBounds="True">
            <TextBox
                x:Name="sampleAnimation"
                HorizontalAlignment="Center"
                VerticalAlignment="Center"
                FontSize="60"
                FontWeight="Light"
                Foreground="Red"
                RenderTransformOrigin="0.5,0.5"
                Text="Hello!">
                <TextBox.RenderTransform>
                    <RotateTransform Angle="0"/>
                </TextBox.RenderTransform>
            </TextBox>
        </Grid>
        <TextBlock Grid.Row="2" FontSize="25" Text="Mirrored content using VisualBrush:"/>
        <Grid Grid.Row="3" Background="{StaticResource contentBrush}">
        </Grid>
    </Grid>
</Page>

答案 1 :(得分:0)

使用可视笔刷作为矩形的填充。

你将无法与它进行交互......但这就是完成任务栏上预览缩略图的方式。

<Grid HorizontalAlignment="Left" Name="A" Height="100" Width="100">
    <Grid.Background>
        <SolidColorBrush Opacity="0" Color="White"/>
    </Grid.Background>
    <!-- Contents -->
</Grid>


<Rectangle Name="RA" VerticalAlignment="Top" Width="100" Height="100" HorizontalAlignment="Left" Stroke="Black">
    <Rectangle.Fill>
        <!-- Creates the reflection. -->
        <VisualBrush  AutoLayoutContent="True"  Visual="{Binding ElementName=A}" ViewboxUnits="RelativeToBoundingBox" ViewportUnits="RelativeToBoundingBox" Stretch="Fill" AlignmentX="Left" AlignmentY="Top" Viewport="0,0,1,1" Viewbox="0,0,1,1" TileMode="None">
        </VisualBrush>
    </Rectangle.Fill>
</Rectangle>

要进行交互,您必须将所有属性绑定到相同的屏幕,并使用布局转换来缩小它。

<StackPanel>
    <Grid>
        <TextBox Name="A"/>
    </Grid>
    <Grid>
        <Grid.LayoutTransform>
            <ScaleTransform CenterX=".5" CenterY=".5"  ScaleX=".25" ScaleY=".25"/>
        </Grid.LayoutTransform>
        <TextBox Name="B" Text="{Binding ElementName=A, Path=Text, UpdateSourceTrigger=PropertyChanged}"/>
    </Grid>
</StackPanel>