简体   繁体   English

如何在wp8 c#中更改动态按钮的位置

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

In my windows phone application I am creating a dynamic button like below: 在Windows Phone应用程序中,我正在创建一个动态按钮,如下所示:

    <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 以下是xaml.cs页面代码

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 并将按钮添加到列表框lb_groupofcontacts但是当我创建另一个动态按钮时,它将映射到上一个按钮,这意味着它不会更改按钮的位置

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). 您可以通过将Button的Margin属性设置为不同的值来操纵按钮的位置(第一个按钮为“ 0,0”,第二个按钮为“ 0,200”,依此类推)。

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. 我建议将ListBox替换为具有6个单元格的Grid,这是最简单的决定。

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. 首先,您要在Button_click中创建列表对象,因此每次创建新列表时, only new one button添加only new one button ,并将该列表作为listbox's Content因此您将无法添加第二个按钮。

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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