动态地添加多个按钮到WPF窗口? [英] Dynamically add multiple buttons to wpf window?

查看:146
本文介绍了动态地添加多个按钮到WPF窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将如何添加多个按钮,在C#中的窗口?这里就是我需要做的......我从字典中获得多个用户值(在合理范围内,只有5-6 @值)。每个值,我需要创建一个按钮。现在,我怎么命名按钮,而不是按钮中的文本?我如何定义每个按钮(他们都将是不同的)中的点击的方法?我如何删除按钮,如果我不想要它了?

how would i add multiple buttons to a window in c#? here's what i need to do... i'm getting multiple user values from a dictionary (within reason, only @ 5-6 values). for each value, i need to create a button. now, how do i name the button, not the text within the button? how do i define the "click" method for each button (they will all be different)? and how do i erase the button if i don't want it anymore?

推荐答案

考虑你有一个的StackPanel 名为SP

Consider you have a StackPanel named sp

for(int i=0; i<5; i++)
{
    System.Windows.Controls.Button newBtn = new Button();

    newBtn.Content = i.ToString();
    newBtn.Name = "Button" + i.ToString();

    sp.Children.Add(newBtn);
}

要删除按钮,你可以做

sp.Children.Remove((UIElement)this.FindName("Button0"));

希望这有助于。

Hope this help.

这篇关于动态地添加多个按钮到WPF窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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