WPF。 MVVM。将子项添加到dataTemplate

时间:2015-12-17 13:03:41

标签: c# wpf mvvm

我想使用dataTemplate显示来自四个不同相机的视频源。我的datatemplate是这样的:

<DataTemplate x:Key="MultiViewTemplate">
    <Grid x:Name="MultiViewGrid" ShowGridLines="True" >
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid x:Name="GridVideo1" Grid.Column="0" Grid.Row="0" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
            <Image x:Name="imageMultiVideoStreaming1" Margin="1" Source="/app;component/Images/No_image_available.png" />
        </Grid>
        <Grid x:Name="GridVideo2" Grid.Column="1" Grid.Row="0" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
            <Image x:Name="imageMultiVideoStreaming2" Margin="1" Source="/app;component/Images/No_image_available.png" />
        </Grid>
        <Grid x:Name="GridVideo3" Grid.Column="0" Grid.Row="1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
            <Image x:Name="imageMultiVideoStreaming3" Margin="1" Source="/app;component/Images/No_image_available.png" />
        </Grid>
        <Grid x:Name="GridVideo4" Grid.Column="1" Grid.Row="1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
            <Image x:Name="imageMultiVideoStreaming4" Margin="1" Source="/app;component/Images/No_image_available.png" />
        </Grid>
    </Grid>
</DataTemplate>

...

<Grid x:name"MainGrid" >
   <ContentControl x:Name="CameraContainer" Content="{Binding}" ContentTemplate=Value="{StaticResource MultiViewTemplate}" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" />
</Grid>

开始时,当我运行应用程序时,每个网格都应显示图像No_image_avaiable.png。它的工作正确。之后,我尝试连接相机,每个视频源都应该显示在GridVideo1,GridVideo2 ......

如果我在代码隐藏中执行此操作,我可以看到来自摄像头1的视频......

MainGrid.Children.Add(VideoStreamingGrid1)

但我想做这样的事情:

    ...GridVideo1.Children.Add(VideoStreamingGrid1)
    ...GridVideo2.Children.Add(VideoStreamingGrid2)
    ...GridVideo3.Children.Add(VideoStreamingGrid3)
    ...GridVideo4.Children.Add(VideoStreamingGrid4)

我该怎么做?

感谢。

0 个答案:

没有答案