以编程方式使用自定义元素创建网格

发布于 2024-12-04 12:15:26 字数 5199 浏览 1 评论 0原文

我正在尝试以编程方式创建一个网格,并将自定义控件作为子控件附加到网格中,作为 2x2 矩阵中的第 0 行第 0 列。为了让事情变得更棘手,我使用了 MVVM 设计模式。这里有一些代码可以帮助大家理解这个想法:

App.xaml.cs

base.OnStartup(e);
var viewModel = new MainWindowViewModel();
var mainWindow = new MainWindow();
mainWindow.GridWindows = viewModel.Window.GridWindows;

MainWindowViewModel - 方法返回 GridWindows。

    private Grid CreateGrid()
    {
        Grid grid = new Grid();

        // Create column definitions.
        ColumnDefinition columnDefinition1 = new ColumnDefinition();
        ColumnDefinition columnDefinition2 = new ColumnDefinition();
        columnDefinition1.Width = new GridLength(640);
        columnDefinition2.Width = new GridLength(640);

        // Create row definitions.
        RowDefinition rowDefinition1 = new RowDefinition();
        RowDefinition rowDefinition2 = new RowDefinition();
        rowDefinition1.Height = new GridLength(340);
        rowDefinition2.Height = new GridLength(340);

        // Attached definitions to grid.
        grid.ColumnDefinitions.Add(columnDefinition1);
        grid.ColumnDefinitions.Add(columnDefinition2);
        grid.RowDefinitions.Add(rowDefinition1);
        grid.RowDefinitions.Add(rowDefinition2);

        // Create preview window.
        Border border = new Border();
        border.BorderThickness = new Thickness(20);
        border.Padding = new Thickness(8);
        border.SetResourceReference(Control.BackgroundProperty, "PreviewWindow");

        MediaRTSPElement previewElement = new MediaRTSPElement();
        previewElement.Name = "RTSPStreamPlayer";
        previewElement.Stretch = Stretch.UniformToFill;
        previewElement.Source = "rtsp://192.100.100.22/media/video1";
        previewElement.VideoRenderer = VideoRendererType.EnhancedVideoRenderer;
        previewElement.LoadedBehavior = WPFEVR.DirectShow.Players.MediaState.Play;
        previewElement.SpeedRatio = 0.5;

        //border.Child = previewElement;

        // Add preview window.
        for (int i = 0; i < 4; i++)
        {
            grid.Children.Add(previewElement as UIElement);
            Grid.SetColumn(previewElement, i);
            Grid.SetRow(previewElement, i);
            break;
        }

        return grid;
    }

以及网格应分配给的 XAML 标记< /strong>

<Grid x:Name="GridWindows"></Grid>

问题是我的自定义控件没有出现在网格布局中,这是在没有代码隐藏的情况下执行此操作的 xaml 代码,并且这确实有效:

        <Grid x:Name="GridWindows">
            <!--<Grid.ColumnDefinitions>
                <ColumnDefinition Width="640" />
                <ColumnDefinition Width="640" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="340" />
                <RowDefinition Height="340" />
            </Grid.RowDefinitions>
            <Border BorderThickness="20" Padding="8" Background="{DynamicResource ResourceKey=PreviewWindow}" Grid.Row="0" Grid.Column="0">
                <evr:MediaRTSPElement x:Name="RTSPStreamPlayer"
                              Stretch="UniformToFill"
                              VideoRenderer="EnhancedVideoRenderer"
                              LoadedBehavior="Play"
                              Source="rtsp://192.100.100.22/media/video1"
                              SpeedRatio="0.5" />
            </Border>
            <Border BorderThickness="20" Padding="8" Background="{DynamicResource ResourceKey=PreviewWindow}" Grid.Row="0" Grid.Column="1">
                <evr:MediaRTSPElement x:Name="RTSPStreamPlayer2"
                              Stretch="UniformToFill"
                              VideoRenderer="EnhancedVideoRenderer"
                              LoadedBehavior="Play"
                              Source="rtsp://192.100.100.78/media/video1"
                              SpeedRatio="0.5" />
            </Border>
            <Border BorderThickness="20" Padding="8" Background="{DynamicResource ResourceKey=PreviewWindow}" Grid.Row="1" Grid.Column="0">
                <evr:MediaRTSPElement x:Name="RTSPStreamPlayer3"
                              Stretch="UniformToFill"
                              VideoRenderer="EnhancedVideoRenderer"
                              LoadedBehavior="Play"
                              Source="rtsp://192.100.100.78/media/video1"
                              SpeedRatio="0.5" />
            </Border>
            <Border BorderThickness="20" Padding="8" Background="{DynamicResource ResourceKey=PreviewWindow}" Grid.Row="1" Grid.Column="1">
                <evr:MediaRTSPElement x:Name="RTSPStreamPlayer4"
                              Stretch="UniformToFill"
                              VideoRenderer="EnhancedVideoRenderer"
                              LoadedBehavior="Play"
                              Source="rtsp://192.100.100.22/media/video1"
                              SpeedRatio="0.5" />
            </Border>-->
        </Grid>

关于为什么编程代码不起作用的任何想法?

I'm trying to create a grid programmatically and appending a custom control as a child to the grid as row 0 column 0 out of a 2x2 matrix. To make matters more tricky, I'm using the MVVM design pattern. Heres some code to help everyone get the idea:

App.xaml.cs

base.OnStartup(e);
var viewModel = new MainWindowViewModel();
var mainWindow = new MainWindow();
mainWindow.GridWindows = viewModel.Window.GridWindows;

MainWindowViewModel - method returns the GridWindows.

    private Grid CreateGrid()
    {
        Grid grid = new Grid();

        // Create column definitions.
        ColumnDefinition columnDefinition1 = new ColumnDefinition();
        ColumnDefinition columnDefinition2 = new ColumnDefinition();
        columnDefinition1.Width = new GridLength(640);
        columnDefinition2.Width = new GridLength(640);

        // Create row definitions.
        RowDefinition rowDefinition1 = new RowDefinition();
        RowDefinition rowDefinition2 = new RowDefinition();
        rowDefinition1.Height = new GridLength(340);
        rowDefinition2.Height = new GridLength(340);

        // Attached definitions to grid.
        grid.ColumnDefinitions.Add(columnDefinition1);
        grid.ColumnDefinitions.Add(columnDefinition2);
        grid.RowDefinitions.Add(rowDefinition1);
        grid.RowDefinitions.Add(rowDefinition2);

        // Create preview window.
        Border border = new Border();
        border.BorderThickness = new Thickness(20);
        border.Padding = new Thickness(8);
        border.SetResourceReference(Control.BackgroundProperty, "PreviewWindow");

        MediaRTSPElement previewElement = new MediaRTSPElement();
        previewElement.Name = "RTSPStreamPlayer";
        previewElement.Stretch = Stretch.UniformToFill;
        previewElement.Source = "rtsp://192.100.100.22/media/video1";
        previewElement.VideoRenderer = VideoRendererType.EnhancedVideoRenderer;
        previewElement.LoadedBehavior = WPFEVR.DirectShow.Players.MediaState.Play;
        previewElement.SpeedRatio = 0.5;

        //border.Child = previewElement;

        // Add preview window.
        for (int i = 0; i < 4; i++)
        {
            grid.Children.Add(previewElement as UIElement);
            Grid.SetColumn(previewElement, i);
            Grid.SetRow(previewElement, i);
            break;
        }

        return grid;
    }

And the XAML Markup that the grid should assign to

<Grid x:Name="GridWindows"></Grid>

The problem is my custom control does not appear in the grid layout, heres the xaml code that does it without code-behind, and this does work:

        <Grid x:Name="GridWindows">
            <!--<Grid.ColumnDefinitions>
                <ColumnDefinition Width="640" />
                <ColumnDefinition Width="640" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="340" />
                <RowDefinition Height="340" />
            </Grid.RowDefinitions>
            <Border BorderThickness="20" Padding="8" Background="{DynamicResource ResourceKey=PreviewWindow}" Grid.Row="0" Grid.Column="0">
                <evr:MediaRTSPElement x:Name="RTSPStreamPlayer"
                              Stretch="UniformToFill"
                              VideoRenderer="EnhancedVideoRenderer"
                              LoadedBehavior="Play"
                              Source="rtsp://192.100.100.22/media/video1"
                              SpeedRatio="0.5" />
            </Border>
            <Border BorderThickness="20" Padding="8" Background="{DynamicResource ResourceKey=PreviewWindow}" Grid.Row="0" Grid.Column="1">
                <evr:MediaRTSPElement x:Name="RTSPStreamPlayer2"
                              Stretch="UniformToFill"
                              VideoRenderer="EnhancedVideoRenderer"
                              LoadedBehavior="Play"
                              Source="rtsp://192.100.100.78/media/video1"
                              SpeedRatio="0.5" />
            </Border>
            <Border BorderThickness="20" Padding="8" Background="{DynamicResource ResourceKey=PreviewWindow}" Grid.Row="1" Grid.Column="0">
                <evr:MediaRTSPElement x:Name="RTSPStreamPlayer3"
                              Stretch="UniformToFill"
                              VideoRenderer="EnhancedVideoRenderer"
                              LoadedBehavior="Play"
                              Source="rtsp://192.100.100.78/media/video1"
                              SpeedRatio="0.5" />
            </Border>
            <Border BorderThickness="20" Padding="8" Background="{DynamicResource ResourceKey=PreviewWindow}" Grid.Row="1" Grid.Column="1">
                <evr:MediaRTSPElement x:Name="RTSPStreamPlayer4"
                              Stretch="UniformToFill"
                              VideoRenderer="EnhancedVideoRenderer"
                              LoadedBehavior="Play"
                              Source="rtsp://192.100.100.22/media/video1"
                              SpeedRatio="0.5" />
            </Border>-->
        </Grid>

Any ideas as to why programmatic code isn't working?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

溺孤伤于心 2024-12-11 12:15:26

如果您在 xaml 中创建Grid,则以后无法在代码中设置它。网格(实例)已经在 VisualTree 中。覆盖变量不会产生任何效果。您应该将 Grid 设置为 xaml 定义的控件的内容。我猜你的代码看起来像这样:

代码:

this.GridWindows = createdGrid;

Xaml:

<Grid x:Name="GridWindows"></Grid>

在代码中你应该有这样的东西:

this.GridWindows.Children.Add(createdGrid);

if you're creating Grid in the xaml you can't later set it in code. Grid (instance) is already in visualtree. Overwriting variable won't do any effect. You should set your Grid as content of xaml defined control. I'm guessing that your code looks like this:

Code:

this.GridWindows = createdGrid;

Xaml:

<Grid x:Name="GridWindows"></Grid>

In code you should have something like this:

this.GridWindows.Children.Add(createdGrid);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文