生成TextBlock从特定的后面的代码生成

时间:2013-01-22 12:39:30

标签: c# .net wpf code-behind textblock

假设我有grid

<Grid Name="grid1">
<Grid.ColumnDefinitions>
<ColumnDefinition Name="col1" Width="auto">
                                </ColumnDefinition>
                                <ColumnDefinition Name="col2" Width="380">
                                </ColumnDefinition>
                                <ColumnDefinition Name="col3" Width="auto">
                                </ColumnDefinition>
                                <ColumnDefinition Name="col4" Width="200">
                                </ColumnDefinition>
</Grid.ColumnDefinition>
 <Grid.RowDefinitions>
                                <RowDefinition Name="row1" Height="2*">
                                </RowDefinition>
                                <RowDefinition Name="row2" Height="2*">
                                </RowDefinition>
                            </Grid.RowDefinitions>   
 <Grid>

这是2行和4列。我需要在我的代码后面的TextBlockGrid.Row="1"中创建Grid.Column="2"

如何在相应的Grid 中创建它,并将绑定设置为

  myTextBlock1.SetBinding(TextBlock.TextProperty, (new Binding("SelectedItem." + dataGrid1.Columns[5].Header) 
     { ElementName = "dataGrid1" }));

在运行时到textblock

1 个答案:

答案 0 :(得分:2)

试试这个:

    TextBlock MyTextBlock = New TextBlock();
    Grid.SetRow( MyTextBlock, 1 );
    Grid.SetColumn( MyTextBlock, 2 );
    grid1.Children.Add( MyTextBlock );