在WPF中创建动态按钮 [英] Create dynamic buttons in WPF

查看:125
本文介绍了在WPF中创建动态按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候!
我需要创建一个包含100个按钮的网格.按钮名称应为btn_0_0,btn_0_1等.
如何添加动态按钮创建内容,以便它可以按顺序创建按钮并将所有按钮放置在网格中?
进行了大量搜索,但没有找到可以在WPF(VB)中工作的示例.
Googled&同样也搜索了代码项目,找到了一些信息 ,并能够在WinForms中创建它.问题是我必须使用WPF,但那里有所不同.任何人都可以帮忙吗?

Greetings!
I need to create a grid that holds 100 buttons. Button names should be btn_0_0, btn_0_1 ect or similar.
How to add a dynamic button creation, so it would create the buttons under sequence and place all those inside grid?
Searched a lot, but didnt find an example that will work in WPF (VB).
Googled & searched codeproject too, found some information and were able to make it in WinForms. The thing is that I must use WPF, and things are different there. Can anyone help please?

推荐答案

使用DockPanel 显示通过按钮(通过适当的ItemTemplate呈现)的项目的模板化ListBox 可能是一个更好的设计.在MSDN上或在Code Project上阅读有关如何使用ListBox模板的信息.
A templated ListBox that uses a DockPanel to display items that are rendered as buttons (through an appropriate ItemTemplate) may be a far better design. Read up on MSDN or here on Code Project on how you can use ListBox templates.


首先,请理解所有控件都是动态"添加的. XAML仅有助于自动创建窗口设置,但在后台运行时所有操作都是动态完成的.

这里的主要问题是在网格中设置按钮的行和列.使WPF与其他UI库不同的非凡功能是使用依赖项属性,请参见 http://msdn.microsoft.com/en-us/library/ms752914.aspx [ ^ ].

First, please understand that all controls are added "dynamically". XAML only helps to automate creation of the window setup, but behind the scene all is done dynamically, during run time.

The main problem here is to setup row and column of the button in the grid. The unusual feature which makes WPF different from other UI libraries is using dependency properties, see http://msdn.microsoft.com/en-us/library/ms752914.aspx[^].

Button AddButton(string caption, int row, int column, Grid parent) {
    Button button = new Button();
    button.Content = caption;
    parent.Children.Add(button);
    Grid.SetRow(button, row);
    Grid.SetColumn(button, column);
    return button;
}



只需在一个双周期(按行和列)中调用这样的方法即可.

我还应该注意的是,拥有如此多的按钮是设计问题的一个标志.您可以使用Nishant建议的变体.基于TreeView可以有不同的设计,哪些节点定义了所需的操作,因此可以按层次结构进行组织.

—SA



Just call a method like this in a double cycle (by rows and columns).

I should also note that having so many buttons is a sign of the problem of your design. You can use a variant suggested by Nishant. There can be different designs, based on TreeView which nodes define required action, so the can be structured hierarchically.

—SA


这篇关于在WPF中创建动态按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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