wpf Windows Phone 8 应用程序以动态/编程方式在 g 网格/面板中创建按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20659219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 10:15:24  来源:igfitidea点击:

Windows Phone 8 App Dynamically/programmatically create buttons in g grid/Panel

c#wpfxamlwindows-phone-8

提问by Arsal

I have a problem in creating buttons dynamically in windows phone 8 app.

我在 Windows Phone 8 应用程序中动态创建按钮时遇到问题。

I can create these buttons in my Xaml file But not programmatically... here is the snapshot of that file....

我可以在我的 Xaml 文件中创建这些按钮但不能以编程方式...这是该文件的快照....

http://www.4shared.com/photo/Hu1FVCdn/wp8.html

http://www.4shared.com/photo/Hu1FVCdn/wp8.html

I have a grid view at left side and buttons on it.(1,2,3,4,5).. I made these buttons in Xaml file. Not through program.

我在左侧有一个网格视图,上面有按钮。(1,2,3,4,5)..我在 Xaml 文件中制作了这些按钮。不是通过程序。

When Trade button is clicked then these buttons should display (programmatically)....By Handler of Trade Button..

当点击交易按钮时,这些按钮应该显示(以编程方式)....由交易按钮的处理程序..

Here is My Xaml Code..

这是我的 Xaml 代码..

    <Grid x:Name="grid" Height="618" Margin="6,147,0,0" Width="112" HorizontalAlignment="Left" VerticalAlignment="Top">
        <Grid.Background>
            <ImageBrush Stretch="Fill" ImageSource="pannel.png"/>
        </Grid.Background>
        <Button x:Name="a" Content="1" HorizontalAlignment="Left" Margin="-7,-11,-11,563" VerticalAlignment="Bottom" Width="130" RenderTransformOrigin="0.636,0.638" Height="66" BorderThickness="0" d:IsHidden="True">
            <Button.Background>
                <ImageBrush Stretch="Fill" ImageSource="pannel_btn_unselected.png"/>
            </Button.Background>
        </Button>
        <Button x:Name="b" Content="2" HorizontalAlignment="Left" Margin="-7,0,-11,519" VerticalAlignment="Bottom" Width="130" RenderTransformOrigin="0.636,0.638" Height="66" BorderThickness="0" d:IsHidden="True">
            <Button.Background>
                <ImageBrush Stretch="Fill" ImageSource="pannel_btn_unselected.png"/>
            </Button.Background>
        </Button>
        <Button x:Name="c" Content="3" HorizontalAlignment="Left" Margin="-7,0,-11,475" VerticalAlignment="Bottom" Width="130" RenderTransformOrigin="0.636,0.638" Height="66" BorderThickness="0" d:IsHidden="True">
            <Button.Background>
                <ImageBrush Stretch="Fill" ImageSource="pannel_btn_unselected.png"/>
            </Button.Background>
        </Button>
        <Button x:Name="d" Content="4" HorizontalAlignment="Left" Margin="-7,0,-11,431" VerticalAlignment="Bottom" Width="130" RenderTransformOrigin="0.636,0.638" Height="66" BorderThickness="0" d:IsHidden="True">
            <Button.Background>
                <ImageBrush Stretch="Fill" ImageSource="pannel_btn_unselected.png"/>
            </Button.Background>
        </Button>
        <Button x:Name="e" Content="5" HorizontalAlignment="Left" Margin="-7,0,-11,387" VerticalAlignment="Bottom" Width="130" RenderTransformOrigin="0.636,0.638" Height="66" BorderThickness="0" d:IsHidden="True">
            <Button.Background>
                <ImageBrush Stretch="Fill" ImageSource="pannel_btn_unselected.png"/>
            </Button.Background>
        </Button>
    </Grid>

Except creating like this..I just want to put a for loop (C# File)in Trade Button handler then This work will done Programmatically..

除了像这样创建..我只想在 Trade Button 处理程序中放置一个 for 循环(C# 文件)然后这项工作将以编程方式完成..

I did it but it shows only one button..Not all buttons..? may b location issue..??

我做到了,但它只显示一个按钮..不是所有的按钮..?可能是 b 位置问题.. ??

Here is My xaml.cs code.

这是我的 xaml.cs 代码。

 public main()
  {
    InitializeComponent();

  }
private void Button_Click(object sender, RoutedEventArgs e)
{
    NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.RelativeOrAbsolute));
}
private void Grid_View_Btn_1_Click(object sender, System.Windows.RoutedEventArgs e)
{
           int i;
           for (i=0;i<5;i++)
             {
                Button btn = new Button() { Content = "Button" };
                btn.Width=130;
                btn.Height = 66;
                 grid.Children.Add(btn);
             }

    //       Grid.SetRow(control, i);
    //    Grid.SetColumn(control, j);
    // TODO: Add event handler implementation here.
}

how my result become like my snapshot...But Dynamically/Programmatically. Kindly guide me for this problm..Thanks in advance..!

我的结果如何变得像我的快照......但动态/编程。请指导我解决这个问题..提前致谢..!

回答by marcin93w

Jahind answer is good solution but I think there is little mistake, you need to add the panel to the grid after the loop, something like that should work:

Jahind 的答案是很好的解决方案,但我认为没有什么错误,您需要在循环后将面板添加到网格中,这样的事情应该可以工作:

    private void Grid_View_Btn_1_Click(object sender, System.Windows.RoutedEventArgs e)
    {   
        Dispatcher.BeginInvoke(() => {         
            StackPanel panel = new StackPanel();
            panel.Orientation = System.Windows.Controls.Orientation.Vertical;
            int i;
            for (i=0;i<5;i++)
            {
                Button btn = new Button() { Content = "Button" };
                btn.Width=130;
                btn.Height = 66;
                // btn.Margin = new Thickness(0,0,0,0)//try this if you use grid
                //grid.Children.Add(btn);
                panel.Children.Add(btn);
            }

            grid.Children.Add(panel);

            //       Grid.SetRow(control, i);
            //    Grid.SetColumn(control, j);
            // TODO: Add event handler implementation here.
        });
    } 

回答by Jaihind

Your code seems OK no problem in button creation. your code exactly create 5 buttons and add into grid but the only problem is you put all buttons in one location inside grid. here is the solution 1. if you want to use as parent container set the margin of every single button. 2. Create a stackpanel with orientation vertical and add button first into stackpanel then add stackpanel into grid.

您的代码在按钮创建中似乎没问题。您的代码完全创建了 5 个按钮并添加到网格中,但唯一的问题是您将所有按钮放在网格内的一个位置。这是解决方案 1. 如果您想用作父容器,请设置每个按钮的边距。2. 创建一个垂直方向的堆栈面板,首先将按钮添加到堆栈面板中,然后将堆栈面板添加到网格中。

private void Grid_View_Btn_1_Click(object sender, System.Windows.RoutedEventArgs e)
    {   Dispatcher.BeginInvoke(() => {         
         StackPanel panel = new StackPanel();
         panel.Orientation = System.Windows.Controls.Orientation.Vertical;
               int i;
               for (i=0;i<5;i++)
                 {
                    Button btn = new Button() { Content = "Button" };
                    btn.Width=130;
                    btn.Height = 66;
                   // btn.Margin = new Thickness(0,0,0,0)//try this if you use grid
                    //grid.Children.Add(btn);
                 panel.Children.Add(btn);
                 }
            grid.Children.Add(panel);
        //       Grid.SetRow(control, i);
        //    Grid.SetColumn(control, j);
        // TODO: Add event handler implementation here.
         });
    }