1

I have a list of boolean values named Sequence. I want to change the color of a shape based on the values of the list, that is, I want to iterate through the values of the list and fill the shape with a certain color (for example yellow) every time I stumble upon a True value and change the color of the filling (to blue) every time I stumble upon a False value. I have tried doing it like this:

foreach(bool element in Sequence)
{
    if(element){ ellipse.Fill = new SolidColorBrush(Colors.Yellow); }
    else{ ellipse.Fill = new SolidColorBrush(Colors.Blue); }
    int milisecond = 200;
    Thread.Sleep(miliseconds);
}

But still the colors won't alternate.

Here's the XAML:

`<Ellipse x:Name="elipse" Height="100" Margin="151,52,0,0" Stroke="Black" Width="100" /> `

Do I need a trigger?, What am I doing wrong?. Thank you in advance, and excuse my bad english if something seems funny.

1 Answers1

1
foreach(bool element in Sequence)
{
    if(element){ ellipse.Fill = new SolidColorBrush(Colors.Yellow); }
    else{ ellipse.Fill = new SolidColorBrush(Colors.Blue); }
    int milisecond = 200;
    Thread.Sleep(miliseconds);
}

Runs synchronously on the UI thread. Thus, the UI never gets a chance to update the color. Use a timer instead.

Of course, then you can't foreach, as you'd have to keep track of your current index. One way around that would be to keep your current code but replace the Thread.Sleep with an await Task.Delay.

(apologies from Peter Duniho, who added this code example, but this answer just seemed to be crying out for one):

<edit>
The async based one would look something like this (and IMHO is preferable to using a timer):

foreach (bool element in Sequence)
{
    ellipse.Fill = new SolidColorBrush(element ? Colors.Yellow : Colors.Blue);
    await Task.Delay(200);
}

Of course, to be able to use await, the method in which it's used needs to be declared as async. Unfortunately, the original question does not provide the full method nor its signature, but presumably the OP can research the use of async methods themselves and see how to convert.
</edit>

The whole use case seems like it should be re-designed, but one of those solutions should get you farther down this path. Remember, if you are writing code-behind, you are probably doing it wrong.

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
  • I agree with Bradley that it is probably worth looking into whether there's a way to declare this behavior in XAML. That said, I'm not sure what it'd look like; maybe using a `Storyboard` or something? – Peter Duniho Feb 18 '15 at 23:01
  • @PeterDuniho A storyboard *could* work here depending on a bunch of stuff we can't see :(. Thanks for the edit by the way :) – BradleyDotNET Feb 18 '15 at 23:09
  • I am new in WPF, I just dont get it already. I will start over and re-design it, as you suggest. Can you recommend me a book? Thank you! – Mayra García Feb 18 '15 at 23:10
  • 1
    @MayraGarcía: I know how you feel. I still sometimes "don't get it already". :) WPF is a broad API and not always obvious how to use. One thing that's really important to understand is that it's designed in a way that _strongly_ encourages the use of data-binding and XAML declarations for as much of your program as you can. The API is more usable that way, and the toolset is designed to facilitate that (and not so much to facilitate doing everything in code-behind). MSDN has a wealth of info, and IMHO is a good place to start: https://msdn.microsoft.com/en-us/library/ms754130(v=vs.110).aspx – Peter Duniho Feb 18 '15 at 23:14
  • Also good reading: http://stackoverflow.com/questions/1405739/mvvm-tutorial-from-start-to-finish and https://msdn.microsoft.com/en-us/magazine/dd419663.aspx – BradleyDotNET Feb 18 '15 at 23:15
  • Thank you very much both of you PeterDuniho and BradleyDotNET :) . – Mayra García Feb 18 '15 at 23:20