0

Edit:

8 years later, don't use this method !

Invest your time in learning about WPF and data binding.

Need to know where this question came from? Click the edit button.

DeMama
  • 1,134
  • 3
  • 13
  • 32
  • What about putting your buttons in a list or array instead of giving them similar names? This is the purpose of arrays – Benjamin Gruenbaum Mar 14 '13 at 21:06
  • why not just bind the `SPItems` to an `ItemsControl` full of `Buttons`? – sa_ddam213 Mar 14 '13 at 21:07
  • This initial process is max. 8 values (8 buttons), if another button is pressed, the next 8 values will be retrieved from my database... – DeMama Mar 14 '13 at 21:13
  • there seem to be some misunderstandig i think.. I already have my buttons, i just need their content to be set from the list `Button1.Content = "my value1";` `Button2.Content = "my value 2";` etc... – DeMama Mar 14 '13 at 23:41

4 Answers4

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
Community
  • 1
  • 1
WiiMaxx
  • 5,322
  • 8
  • 51
  • 89
  • If i had more rep, i'd gave you a +1, because this answer helped me the most. I needed to use this in my `for loop:` `SubItem1txt.Text = SPItems[i]; i++;` AND `SubItem2txt.Text = SPItems[i]; i++;` etc.. Of course, i have 8 buttons to be filled.. now if `SPItems.Count` has an index less than 8, an exception is thrown. Can i somehow say `if (SPItems.Count == 1) =>> fill 1 button` OR `if (SPItems.Count == 2) =>> fill 2 buttons` ? Thanks btw !! – DeMama Mar 15 '13 at 09:27
  • @DeMama what exeption you gat? because if you just copy past it and replaces `TextBlock` with `Button` the only trouble could be if you have 7 Buttons and `SPItems.Count>7` then you will get a `NullReferencExeptoin` because it doesn't check if `foundButton != null` – WiiMaxx Mar 15 '13 at 09:37
  • 1
    i found the solution, thanks for your help, i posted an answer :) cheers ! – DeMama Mar 15 '13 at 15:35
1

Do NOT manipulate UI elements in code for these simple tasks. This is the WPF way to do what you're asking:

<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>

Code Behind:

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);
        }
    }
}

This is what it looks like in my screen:

enter image description here

Federico Berasategui
  • 43,562
  • 11
  • 100
  • 154
0

This is sort of a dirty solution but if you're going to be manipulating all the buttons on the form it's worth it. The simplest way that I know of is to add all the buttons to an ArrayList (yes, manually, that's the painful part) and then just looping over that list.

evanmcdonnal
  • 46,131
  • 16
  • 104
  • 115
0

Thanks to WiiMaxx i found the solution !

With using this method i succesfully set my buttons text with data retrieved from my database.

It became a foreach loop, but now it works exactly as needed. edit: found the for version

I have a WrapPanel, which contains my buttons, in which again contains my TextBlock's.

Because my TextBlock's don't start with 0 but with 1 (ex: SubItem1txt) i needed to create an extra int. I hope this will be usefull for someone.

Thanks to everyone :) !

foreach version:

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++;
    }

for version:

    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++;
    }
Community
  • 1
  • 1
DeMama
  • 1,134
  • 3
  • 13
  • 32