gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 20:09:57 25 4
gpt4 key购买 nike

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

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

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

我在左侧有一个 GridView ,上面有按钮。(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# 文件)然后这项工作将以编程方式完成..

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

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

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

最佳答案

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

关于c# - Windows Phone 8 应用程序动态/以编程方式在 g 网格/面板中创建按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20659219/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com