Reputation: 125
I'm a little new to this sort of coding, but i am trying to access dynamically created TextBlock
properties (like, TextBlock.Tag, Name, etc) within a StackPanel
every tick of a timer. What i intend to do with each TextBlock
is to see what its tag
property is, and if it matches a conditoinal, for the timer to alter the TextBlock
property in some way.
So it's a matter of finding a way to code every timer Tick: "For every TextBlock.Tag
in StackPanel, if TextBlock.Tag == this
, do this to the TextBlock
."
Here is some code to help visualize what I am doing:
Xaml code:
<StackPanel Name="StackP" Margin="6,0,6,0"/>
C# code:
{
for (var i = 0; i < MaxCountOfResults; ++i)
{
TextBlock SingleResult= new TextBlock { Text = Resultname.ToString(), FontSize = 20, Margin = new Thickness(30, -39, 0, 0) };
//a condition to alter certain TextBlock properties.
if (i == .... (irrelevant to this example))
{
SingleResult.Foreground = new SolidColorBrush(Colors.Yellow);
SingleResult.Tag = "00001";
}
//Add this dynamic TextBlock to the StackPanel StackP
StackP.Children.Add(SingleResult);
}
//the timer that starts when this entire function of adding the TextBlocks to the StackPanel StackP tree is done.
Atimer = new Timer(new TimerCallback(Atimer_tick), 0, 0, 100);
}
public void Atimer_tick(object state)
{
The area where I have no idea how to reference the Children of stackpanel StackP with every timer tick. I need help :(
}
Thank you guys. I am still learning this and the help is needed.
Upvotes: 1
Views: 248
Reputation: 11040
you should be able to use timer, but I'd recommend using BackgroundWorker
to perform a loop instead of timer events, which could collide. Even better - use SilverLight-style animations with triggers.
On the non-UI thread you'd want to use Dispatcher call to invoke your async code back on the UI thread, something like:
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
try
{
foreach (TextBlock txb in StackP.Children){
txb.Text = "xyz";
}
}
catch (Exception ex)
{
Debug.WriteLine("error: "+ex);
}
});
Upvotes: 2