开发者

windows phone 7, adding buttons to panel

Following piece works very well in silverlight,

    <Grid x:Name="ContentPanelInner" Grid.Row="1" Margin="12,0,12,0">
        <StackPanel x:Name="stackPanelInner">
            <Grid VerticalAlignment="Top">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition />                        
                </Grid.ColumnDefinitions>

                <Button Grid.Column=开发者_如何学JAVA"0" Content="Ok" Width="100"/>
                <Button Grid.Column="1" Content="No" Width="100"/>
            </Grid>
        </StackPanel>
    </Grid>

It just adds two buttons in the same row. When I try to do same from C#, it couldn't work.

Here is my c# code:

        Grid g1 = new Grid();
        //StackPanel innerSP = new StackPanel();
        ColumnDefinition cd1 = new ColumnDefinition();            
        ColumnDefinition cd2 = new ColumnDefinition();
        g1.ColumnDefinitions.Add(cd1);
        g1.ColumnDefinitions.Add(cd2);
        panel1.Children.Add(g1);
        Grid.SetColumn(buttonOk,0);
        Grid.SetColumn(buttonNo, 1);

        panel1.Children.Add(buttonOk);            
        panel1.Children.Add(buttonNo);           

        border.Child = panel1;

        // Set the Child property of Popup to the border 
        // which contains a stackpanel, textblock and button.
        p.Child = border;

Can someone tell me what has been wrong?


Change this:

Grid.SetColumn(buttonOk,0);
Grid.SetColumn(buttonNo, 1);

to this:

buttonOk.SetValue(Grid.ColumnProperty, 0);
buttonNo.SetValue(Grid.ColumnProperty, 1);

If the buttons are supposed to be in the Grid columns (which would seem to make sense), then you should also change this:

panel1.Children.Add(buttonOk);
panel1.Children.Add(buttonNo);

to this:

g1.Children.Add(buttonOk);
g1.Children.Add(buttonNo);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜