Как получить доступ по Name к Grid если он в ItemsControl

Я нашел отличный способ добавления удаления колонок и столбцов на сайте MSDN . Подскажите как заставить его работать если Grid с Name внутри шаблона ItemsControl? Он прекрасно работает если код такой:

<Grid DockPanel.Dock="Top" Name="MyGrid1" HorizontalAlignment="Left"  ShowGridLines="true" Width="300" Height="300">
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>
</Grid>

И MyGrid1 вообще не видит если код такой:

<ItemsControl x:Name="MyItemsControl" ItemsSource="{Binding Buttons}" Grid.RowSpan="2">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Grid DockPanel.Dock="Top" Name="MyGrid1" HorizontalAlignment="Left"  ShowGridLines="true" Width="300" Height="300">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition/>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition/>
                    <RowDefinition/>
                </Grid.RowDefinitions>
            </Grid>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemContainerStyle>
        <Style>
            <Setter Property="Grid.Row" Value="{Binding Row}"/>
            <Setter Property="Grid.Column" Value="{Binding Column}"/>
        </Style>
    </ItemsControl.ItemContainerStyle>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Button Content="{Binding Content}" Command="{Binding Command}" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

Уточнение: Как связать с моим ColumnDefinition и RowDefinition ?

 RowDefinition rowDef1;
   ColumnDefinition colDef1;

   private void addCol(object sender, RoutedEventArgs e)
   {
      colDef1 = new ColumnDefinition();
      grid1.ColumnDefinitions.Add(colDef1);
   }

Ответы (0 шт):