更改多个按钮的属性 [英] Change Properties of multiple buttons

查看:79
本文介绍了更改多个按钮的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一款游戏,是wpf的战舰迷你版。我制作了25个按钮而不是5x5网格,现在我想更改按钮的属性。目前我正在通过为每个按钮创建一个click_down事件来更改它们,这是一个耗时的方法,我可以使用循环或单个函数更改属性,我可以每次调用。按钮命名为b1,b2.b3 ... b25。这可能吗?

Hi, I made a game , a mini version of battleship in wpf. I made 25 buttons instead of a 5x5 grid, now I want to change the properties of the buttons. Currently I am changing them by creating a click_down event for each button which is a time consuming method, can I change the properties using loop or a single function which I can call every time. The buttons are named as b1,b2.b3... b25. Is this possible?

推荐答案

我想说不,这是不可能的,但这不是很严重,对吧? :-)



我不知道你是如何创建按钮数组的,但很明显你需要一组数组。我建议你不要使用XAML,因为XAML会创建单独的按钮,而不是数组。但是,我建议您在XAML中创建一个 Grid 的实例,并将其填入后面的代码中。在XAML中执行此操作意味着很多难以维护的愚蠢的手动工作。

I want to say "no, it's impossible", but it cannot be serious, right? :-)

I have no idea how you created the array of buttons, but it's pretty obvious that you need an array of them. I would suggest you don't use XAML for that, because XAML would create individual buttons, not the array. However, I would suggest you create an instance of Grid in XAML and fill it in code behind. Doing it in XAML means to much of dumb manual work which is hard to maintain.
const int matrixSize = 5;
Grid buttonGrid = // can come from XAML
Button[,] buttons = new Button[matrixSize, matrixSize];
for (int yIndex = 0; yIndex < matrixSize; ++yIndex)
    for (int xIndex = 0; xIndex < matrixSize; ++xIndex) {
        buttons[yIndex, xIndex] = new Button(); 
        buttonGrid.SetRow(buttons[yIndex, xIndex], yIndex);
        buttinGrid.SetColumn(buttons[yIndex, xIndex], xIndex);
        // set any button's properties
} //x, y loops

使用 Grid.SetRow Grid.SetColumn 方法是关键;这是将附加属性设置为 UIElement 的实例的方法:

https://msdn.microsoft.com/en-us/library/ms589026%28v=vs。 110%29.aspx [ ^ ],

https://msdn.microsoft.com/en-us/library/system.windows.controls.grid_properties%28v=vs.110%29.aspx [<一个href =https://msdn.microsoft.com/en-us/library/system.windows.controls.grid_properties%28v=vs.110%29.aspxtarget =_ blanktitle =New Window> ^ ]。



参见:

https://msdn.microsoft.com/en-us/library/vstudio/ms749011%28v=vs.100%29.aspx [ ^ ],

https://msdn.microsoft.com/en-us /library/vstudio/ms752914%28v=vs.100%29.aspx [ ^ ]。



如果你这样做,你可以以后随时使用类似的循环更改任何其他属性。



-SA

Using Grid.SetRow and Grid.SetColumn methods is the key; this is the way to set an attached property to an instance of a UIElement:
https://msdn.microsoft.com/en-us/library/ms589026%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.controls.grid_properties%28v=vs.110%29.aspx[^].

See also:
https://msdn.microsoft.com/en-us/library/vstudio/ms749011%28v=vs.100%29.aspx[^],
https://msdn.microsoft.com/en-us/library/vstudio/ms752914%28v=vs.100%29.aspx[^].

If you do it this way, you can use the similar loop any time later to change any other properties.

—SA


这篇关于更改多个按钮的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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