的Windows Phone 8应用程序动态/编程创建摹网/面板按钮 [英] Windows Phone 8 App Dynamically/programmatically create buttons in g grid/Panel

查看:189
本文介绍了的Windows Phone 8应用程序动态/编程创建摹网/面板按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows Phone中动态创建按钮8的应用程序有问题。

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

我可以在我的XAML文件中创建这些按钮,但没有编程...
给的是,文件的快照....

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

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

我在左侧网格视图并在其上的按钮。(1,2,3,4,5)..
我在XAML文件中做出这些按钮。不通过程序。

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.

在交易按钮被点击然后这些按钮应显示(编程)......通过贸易巴顿处理器。

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

下面是我的XAML代码..

Here is My Xaml Code..

    <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>



除了创建像this..I只想把一个for循环(C#文件)的交易按钮处理器则这项工作将完成编程..

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

我做到了,但它表明只有一个button..Not所有按钮..?
可B地理位置问题??

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

下面是我的xaml.cs代码。

Here is My xaml.cs code.

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



我的结果如何变得像我的快照...但动态/编程。
请指导我这个problm..Thanks提前..!

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

推荐答案

Jahind答案是很好的解决方案,但我觉得有一点差错,你需要在循环之后到面板添加到网格中,这样的事情应该工作:

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:

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

这篇关于的Windows Phone 8应用程序动态/编程创建摹网/面板按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆