如何在DataTemplate中向Grid添加控件

时间:2014-12-01 11:21:31

标签: c# wpf

我有Xml代码:

        <**Grid Name="myGrid"** Background="#FFE9F4F8" Margin="0,4" ShowGridLines="False"  >
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="60"/>
                <ColumnDefinition Width="60"/>
                <ColumnDefinition Width="120"/>
                <ColumnDefinition Width="140"/>

            </Grid.ColumnDefinitions>

            <TextBlock Grid.Column="0" Text="Character"/>

            <ComboBox Grid.Column="1"   x:Name="comboBoxChar">
                <ComboBoxItem Content="blue 1"/>
                <ComboBoxItem Content="blue 2"/>
            </ComboBox>

            <TextBlock Grid.Column="2" Text="{Binding Path=behaviorName}" Width="Auto" HorizontalAlignment="Center" />

            <ComboBox Grid.Column="3" x:Name="comboBoxBehavior" ItemsSource="{Binding  Path=comboList}" Visibility="{Binding Path=visible}"/>


        </Grid>
    **</DataTemplate>**

 

我想在c#代码中为此Grid(名为“myGrid”)添加一些控件(例如textBox)。 怎么做?

1 个答案:

答案 0 :(得分:0)

TextBlock txt = new TextBlock();
txt.Width = 200;
txt.Height = 100;
myGrid.Children.Add(txt) ;