【问题标题】:How to dynamically add buttons to ListBox - WFP - C#如何将按钮动态添加到 ListBox - WPF - C#
【发布时间】:2013-09-11 07:48:07
【问题描述】:

我想通过ListBox末尾的按钮动态添加按钮到动态添加的ListBox中。

我想要带有按钮的 ListBox,每个按钮都有不同的索引。因为在那之后我必须把事件放到那个按钮上。所以按钮将带有索引。

在创建的 ListBox 下将是按钮,点击后可以添加新的 ListBox 按钮(与第一个示例相同)。

我该怎么做?

感谢您的帮助!

【问题讨论】:

  • 你已经尝试过了吗?
  • 是的,我尝试了一些方法,但没有任何效果..
  • @user2740185 请粘贴一些代码
  • 看看我的编辑。我认为这是解决方案。 :)
  • @Martin Gabriel 你应该单独发布你的答案并接受它作为答案)

标签: c# wpf button dynamic listbox


【解决方案1】:

这里是解决方案:

private Boolean addNewListBox(ListBox targetElement, int indexOfElement)
    {
        ListBox elementListBox = new ListBox();

        elementListBox.Name = "elementListBox" + indexOfElement;
        elementListBox.VerticalAlignment = VerticalAlignment.Top;

        targetElement.Items.Remove(addFloor);
        targetElement.Items.Add(elementListBox);
        elementListBox.Items.Add(putElements("TEST", index));
        targetElement.Items.Add(addFloor);
        return true;
    }
public object putElements(string nameOfElement, int indexOfElement)
    {
        if (nameOfElement.Contains("TEST"))
        {
            Button floorButton = new Button();

            floorButton.Name = "floorButton" + indexOfElement;
            floorButton.Content = "floorButton" + indexOfElement;
            floorButton.Height = 60;
            floorButton.Width = 87;
            floorButton.Margin = new Thickness(0, 2, 0, 0);

            return floorButton;
        }
    }

当然是按钮上的事件。 ;)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-21
    • 2010-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多