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.
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.
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];
}
}
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
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:
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.
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 :) !
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++;
}