Creating RowDefinitions and ColumnDefinitions in code

牧云@^-^@ 提交于 2019-12-23 19:52:32

问题


I developing application for windows-phone, I want to create table with 2 rows an 2 columns I create xaml code for this table

<Grid Background="White">   
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/> 
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition /> 
    </Grid.RowDefinitions/>      
</Grid>

I want to create this Grid in code

Grid chat_userpicgrid = new Grid();
newgrid.Children.Add(chat_userpicgrid);

But I don't know how to create RowDefinitions and ColumnDefinitions.


回答1:


Grid newGrid = new Grid();
newGrid.Background = new SolidColorBrush(Colors.White);
newGrid.ColumnDefinitions.Add(new ColumnDefinition());
newGrid.ColumnDefinitions.Add(new ColumnDefinition());
newGrid.RowDefinitions.Add(new RowDefinition());
newGrid.RowDefinitions.Add(new RowDefinition());

To position elements in specific cells:

Grid chat_userpicgrid = new Grid();
Grid.SetColumn(chat_userpicgrid, 1);
Grid.SetRow(chat_userpicgrid, 1);
newGrid.Children.Add(chat_userpicgrid);

Have a look at the code at the bottom of this MSDN page



来源:https://stackoverflow.com/questions/12108405/creating-rowdefinitions-and-columndefinitions-in-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!