TextBlock foreground programmatically changimg problems RRS feed

  • Question

  • Hello,

    In my case I have to change both background style for Canvas and foreground for a TextBlock that is in ScrollView that is in that Canvas. It seems as follows:

    List<int> redNames = new List<int>();

    Canvas LibraryPage = new Canvas();

    TextBlock[] tb = new TextBlock[length];

    Canvas[] line = new Canvas[length];

    ScrollViewer[] sv = new ScrollViewer[length];

    for(int i = 0; i < length; i++) { tb[i] = new TextBlock(); tb[i].Style = GreenFontStyle; tb[i].Text = "text"; ScrollViewer sv[i] = new ScrollViewer(); sv[i].Content = tb[i]; line[i] = new Canvas(); line[i].Name = "" + i; line[i].Style = YellowBackgroundStyle; line[i].Tap += line_Tap; line[i].Children.Add(sv[i]); LibraryPage.Children.Add(line[i]); }

    It was a dynamically building module, and in this way I am trying to change parameters:

    void line_Tap(object sender, GestureEventArgs e)
        int indx = Convert.ToInt32(((FrameworkElement)sender).Name);
        if (redNames.Contains(indx))
             tb[indx].Style = GreenFontStyle;
             line[indx].Style = YellowBackgroundStyle;
             tb[indx].Style = YellowFontStyle;
             line[indx].Style = RedBackgroundStyle;

    It seems, like by tapping on the "line" we should change both of parameters simultaneously, but in reality, when tapping on canvas "line[i]", its background style is changing correctly, but a TextBlock's (tb[i]) foreground changing is not correct. Only first tapped element changes foreground to yellow, but others - not. In addition, the first ones does not change its foreground back.

    But... without a ScrollView everything works nice - both of parameters are changing simultaneously!

    What is my problem?


    • Edited by leozel Tuesday, April 8, 2014 6:04 AM
    Sunday, April 6, 2014 10:23 PM

All replies

  • Did you cut and paste that code? ... there appear to be a couple syntax and logical errors.


    Monday, April 7, 2014 5:49 PM
  • It's a short variant, I left only the necessary for this post code, without addition properties, like Margin, Height etc. As to errors, than I found only "ScrollViewe();" instead of "ScrollViewer();" and I corrected it. Plus to this, I've added one line: "Canvas LibraryPage = newCanvas();", to make the code more undestandable. I hope that's all. If you found more errors, please tell me.


    Monday, April 7, 2014 6:19 PM
  • The other thing which looked like an error is this line:


    If you want one TextBlock per line I would expect this to be:



    Monday, April 7, 2014 10:03 PM
  • You're right, thank you. In original code it appears like 'sv[i]', it could not pass debug in other way. A problem is that the style of TextBlocks tb[i] is changing correctly only without ScrollViewers (sv[i]): line[i].Children.Add(tb[i]);


    Tuesday, April 8, 2014 6:14 AM