简体   繁体   中英

How to change the position of dynamic buttons in wp8 c#

In my windows phone application I am creating a dynamic button like below:

    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="24,0,0,0">
        <TextBox x:Name="tb_groupname"
             Height="90"
             Background="White" 
             Margin="0,0,125,517"

             Foreground="Blue" TextChanged="tb_groupname_TextChanged" GotFocus="tb_groupname_GotFocus" LostFocus="tb_groupname_LostFocus"/>
        <Button x:Name="btn_groupname"
                Content="Add"
                Background="AliceBlue"
                Foreground="Blue"
                FontSize="25"
                Width="120"
                Height="90"
                HorizontalAlignment="Right"
                VerticalAlignment="Top" Click="btn_groupname_Click"></Button>
        <ListBox x:Name="lb_groupofcontacts" ItemsSource="{Binding}" Margin="0,118,0,0">

        </ListBox>

And below is the xaml.cs page code

private void btn_groupname_Click(object sender, RoutedEventArgs e)
{
    if (tb_groupname.Text != string.Empty)
    {
        List<Button> buttons = new List<Button>();   

            Button btn = new Button();// Cast the control to Button.
            btn.Content = tb_groupname.Text;
            btn.Width = 200;
            btn.Height = 200;
            btn.Click += new RoutedEventHandler(btn_Click); // Add event to button.
            buttons.Add(btn);
            buttonName = tb_groupname.Text;
            tb_groupname.Text = string.Empty;

        lb_groupofcontacts.DataContext = buttons;
    }

}

And add button into listbox lb_groupofcontacts but when I create another dynamic button it map onto the previous button means it not change the position of the button

And I want that

first button created on top left
second button created on top right
third button created on center left
forth button created on center right
fifth button created on bottom left
sixth button created on botton right

Kindly suggest me how do I change the position of the like above or if you know any other way to change the position of dynamic both then tell me, waiting for your reply. Thanks.

You can manipulate Button's position by setting its Margin property to different values ("0, 0" for the first button, "0, 200" for the second one and so on).

But that's not very good approach, the right way depends on what you need to do. I'd suggest to replace ListBox with Grid having 6 cells as the most simple decision.

First of all You are creating object of list into Button_click so every time it is creating new list and adds only new one button and taking that list as listbox's Content so you are not able to add Second Button.

You can set Position of Button Dynamically by:

btn.Margin = new Thickness(LEFT,TOP,RIGHT,BOTTOM);

OR

btn.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
btn.VerticalAlignment = System.Windows.VerticalAlignment.Center;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM