0

编辑:

8年后,不要使用这种方法!

花时间学习 WPF 和数据绑定。

想知道这个问题是从哪里来的吗?单击编辑按钮。

4

4 回答 4

2

If your Buttons Named like Button1,Button2,Button3....

you could use FindChild<T>()
here you get the method

for(int i =0; i< SPItems.Count;i++) // iterates for each item in your SPItems
{
     Button foundButton =  UIHelper.FindChild<Button>(Application.Current.MainWindow, "Button"+i);

     if(foundButton != null) //Protecteted agains trying to use Null-Reference
     {
          var foundButtonTextBlock =foundButton .Content as TextBlock

          if(foundButtonTextBlock != null)//Protecteted agains trying to add Text by Null-Reference
               foundButtonTextBlock.Text  = SPItems[i];
     }
}

Edit

as normal way:

 for(int i =0; i< SPItems.Count;i++) // iterates for each item in your SPItems
    {
         TextBlock foundTextBlock =  UIHelper.FindChild<TextBlock>(this, "SubItem"+1+i+"txt");

         if(foundTextBlock != null) //Protecteted agains trying to add Text by Null-Reference
         {
              foundTextBlock .Text  = SPItems[i];
         }
    }

as Painful way:

int i =0;
if(SPItems.Count >0)
{
    SubItem1txt.Text = SPItems[i]; 
    i++;
}
if(SPItems.Count >1)
{
    SubItem2txt.Text = SPItems[i];
    i++;
}
if(SPItems.Count >2)
{
    SubItem3txt.Text = SPItems[i];
    i++;
}
if(SPItems.Count >3)
{
    SubItem4txt.Text = SPItems[i]; 
    i++;
}
if(SPItems.Count >4)
{
    SubItem5txt.Text = SPItems[i]; 
    i++;
}
if(SPItems.Count >0)
{
    SubItem6txt.Text = SPItems[i];
    i++;
}
if(SPItems.Count >5)
{
    SubItem7txt.Text = SPItems[i];
    i++;
}
if(SPItems.Count >6)
{
    SubItem8txt.Text = SPItems[i];
    i++;
}
//..... never ever do this
于 2013-03-15T08:19:02.943 回答
1

不要在代码中为这些简单任务操作 UI 元素。这是执行您所要求的 WPF 方式:

<Window x:Class="WpfApplication4.Window18"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window18" Height="300" Width="300">
    <ItemsControl ItemsSource="{Binding}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Button Content="{Binding}" Margin="2"/>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Window>

代码背后:

using System.Windows;
using System.Linq;

namespace WpfApplication4
{
    public partial class Window18 : Window
    {
        public Window18()
        {
            InitializeComponent();

            DataContext = Enumerable.Range(0, 20).Select(x => "Item" + x);
        }
    }
}

这是它在我的屏幕上的样子:

在此处输入图像描述

于 2013-03-14T21:21:27.170 回答
0

感谢WiiMaxx ,我找到了解决方案!

通过使用这种方法,我成功地使用从我的数据库中检索到的数据设置了我的按钮文本。

它变成了一个foreach循环,但现在它完全按照需要工作。编辑:找到for版本

我有一个WrapPanel,其中包含 my buttons,其中又包含 my TextBlock's

因为我TextBlock's不是从(例如:)开始,0所以我需要创建一个额外的. 我希望这对某人有用。1SubItem1txtint

谢谢大家 :) !

foreach 版本:

int i = 1;
int a = 0;
foreach (string spi in SPItems)
    {
        WrapPanel pnl = UIHelper.FindChild<WrapPanel>(this, "SubItems");
        Button btn = UIHelper.FindChild<Button>(pnl, "SubItem" + i);
        TextBlock tb = UIHelper.FindChild<TextBlock>(btn, "SubItem" + i + "txt");
        btn.Visibility = Visibility.Visible;
        tb.Text = SPItems[a];
        a++;
        i++;
    }

对于版本:

    int i = 1;
    for (int a = 0; a < SPItems.Count; a++)
    {
        WrapPanel pnl = UIHelper.FindChild<WrapPanel>(this, "SubItems");
        Button btn = UIHelper.FindChild<Button>(pnl, "SubItem" + i);
        TextBlock tb = UIHelper.FindChild<TextBlock>(btn, "SubItem" + i + "txt");
        btn.Visibility = Visibility.Visible;
        tb.Text = SPItems[a];
        i++;
    }
于 2013-03-15T15:22:57.400 回答
0

这是一种肮脏的解决方案,但如果您要操作表单上的所有按钮,那么它是值得的。我所知道的最简单的方法是将所有按钮添加到一个ArrayList(是的,手动,这是痛苦的部分),然后循环遍历该列表。

于 2013-03-14T21:07:27.550 回答