与编程自定义元素创建网格 [英] Programmatically Create Grid with Custom Element

查看:101
本文介绍了与编程自定义元素创建网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图以编程方式创建一个网格和附加自定义控件作为一个孩子到电网为0行0列了一个2x2矩阵。为了使问题更棘手,我使用的MVVM设计模式。继承人一些代码,以帮助大家明白了吧:



App.xaml.cs



  base.OnStartup(E); 
VAR视图模型=新MainWindowViewModel();
变种的主窗口=新的主窗口();
mainWindow.GridWindows = viewModel.Window.GridWindows;



MainWindowViewModel - 返回GridWindows

$ B $。 b

 专用网CreateGrid()
{
格之格=新的Grid();

//创建列定义。
ColumnDefinition columnDefinition1 =新ColumnDefinition();
ColumnDefinition columnDefinition2 =新ColumnDefinition();
columnDefinition1.Width =新GridLength(640);
columnDefinition2.Width =新GridLength(640);

//创建行定义。
RowDefinition rowDefinition1 =新RowDefinition();
RowDefinition rowDefinition2 =新RowDefinition();
rowDefinition1.Height =新GridLength(340);
rowDefinition2.Height =新GridLength(340);

//附加定义为网格。
grid.ColumnDefinitions.Add(columnDefinition1);
grid.ColumnDefinitions.Add(columnDefinition2);
grid.RowDefinitions.Add(rowDefinition1);
grid.RowDefinitions.Add(rowDefinition2);

//创建预览窗口。
边境边界=新的边界();
border.BorderThickness =新厚度(20);
border.Padding =新厚度(8);
border.SetResourceReference(Control.BackgroundPropertyPreviewWindow);

MediaRTSPElement previewElement =新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;

//添加预览窗口。
的for(int i = 0;我4;;我++)
{
grid.Children.Add(previewElement为的UIElement);
Grid.SetColumn(previewElement,I);
Grid.SetRow(previewElement,I);
中断;
}

返回网格;
}



而XAML标记在网格应该分配给

 <电网X:NAME =GridWindows>< /网格和GT; 



但问题是我自定义的控件不会出现在网格布局,继承人的XAML代码,做它没有代码后面,这样没有问题:

 <电网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填充=8的背景={DynamicResource的ResourceKey = PreviewWindow}Grid.Row =0Grid.Column =0>
< EVR:MediaRTSPElement X:NAME =RTSPStreamPlayer
弹力=UniformToFill
VideoRenderer =EnhancedVideoRenderer
LoadedBehavior =播放
来源= RTSP://192.100.100.22/media/video1
SpeedRatio =0.5/>
< /边框>
< BORDER了borderThickness =20填充=8的背景={DynamicResource的ResourceKey = PreviewWindow}Grid.Row =0Grid.Column =1>
< EVR:MediaRTSPElement X:NAME =RTSPStreamPlayer2
弹力=UniformToFill
VideoRenderer =EnhancedVideoRenderer
LoadedBehavior =播放
来源= RTSP://192.100.100.78/media/video1
SpeedRatio =0.5/>
< /边框>
< BORDER了borderThickness =20填充=8的背景={DynamicResource的ResourceKey = PreviewWindow}Grid.Row =1Grid.Column =0>
< EVR:MediaRTSPElement X:NAME =RTSPStreamPlayer3
弹力=UniformToFill
VideoRenderer =EnhancedVideoRenderer
LoadedBehavior =播放
来源= RTSP://192.100.100.78/media/video1
SpeedRatio =0.5/>
< /边框>
< BORDER了borderThickness =20填充=8的背景={DynamicResource的ResourceKey = PreviewWindow}Grid.Row =1Grid.Column =1>
< EVR:MediaRTSPElement X:NAME =RTSPStreamPlayer4
弹力=UniformToFill
VideoRenderer =EnhancedVideoRenderer
LoadedBehavior =播放
来源= RTSP://192.100.100.22/media/video1
SpeedRatio =0.5/>
< /边框> - >
< /网格和GT;



任何想法,为什么程序代码是不工作?


< DIV CLASS =h2_lin>解决方案

如果你在你以后不能将其设置在代码中创建XAML 电网。网格(实例)已经在的VisualTree。覆盖变量不会做任何影响。你应该设置你的电网如XAML定义控制内容。我猜你的代码如下所示:



代码:

  this.GridWindows = createdGrid; 



XAML中:

 <电网X:NAME =GridWindows>< /网格和GT; 

在代码中,你应该有这样的事情:

  this.GridWindows.Children.Add(createdGrid); 


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?

解决方案

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);

这篇关于与编程自定义元素创建网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆