Windows Phone 8 App以g网格/面板动态/编程创建按钮

时间:2013-12-18 13:13:55

标签: c# wpf xaml windows-phone-8

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

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

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

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

单击“交易”按钮后,这些按钮应显示(以编程方式)....按交易按钮处理程序..

这是我的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>

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

我做了但它只显示了一个按钮..没有所有按钮..? 可能b位置问题.. ??

这是我的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.
}

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

2 个答案:

答案 0 :(得分:4)

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.
        });
    } 

答案 1 :(得分:1)

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

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.
         });
    }