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

查看:64
本文介绍了向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?

推荐答案

假设你有一个名为 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);
}

要删除按钮,您可以这样做

To remove button you could do

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

希望对您有所帮助.

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

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