2

Let us say that I have an empty Textblock :

textblock1.Text = "";

Then I only put Inlines content in it with these two statements:

textblock1.Inlines.Add(new Run() { Text = "A. ", Foreground = Brushes.Red });
textblock1.Inlines.Add(new Run() { Text = responses.Current.Value, Foreground = Brushes.Black});

The amazing stuff is that I can visualize the content properly in my window, however the Text property of the Textblock keeps being empty! This causes a problem because I need to pass the value of this Textblock to an other Textblock.

The other thing I really can't figure out is that when I call my function for second time, the textblock1.Text property is being updated properly ! It is updated properly for every call but the first ! I've spent hours on msdn but I'm really confused. Moreover, I can read that on the website :

The Text property returns a value (the appended text of all Run elements in the InlineCollection). However, the returned value does not include any formatting that has been applied to the Run elements.

I have very carefully checked my code and debugged to see if there were any other place where I manipulate these property, but I haven't found one. If anyone has any idea, to me this thing is becoming senseless...

tobiak777
  • 3,175
  • 1
  • 32
  • 44
  • When and how in your code are you using the value of the Text property? Is it through a binding or direct access to the property? – John Bowen Jun 18 '13 at 13:33
  • Thank you for your answer ! I also use this later in my code to add the value of Textblock1 to an other Textblock textBlock2.Inlines.Add(new Run() { Text = TextBlock11.Text + " ", FontSize = 25, Foreground = couleurSerie }); And that's it ! I do it by direct access in my code-behind – tobiak777 Jun 18 '13 at 13:42
  • 1
    Are you doing this in the View's code-behind constructor? if so move it to the Loaded event of the Window and it should be fine even for the first time. – Viv Jun 18 '13 at 13:44
  • Thank you Viv, it works!! Actually I wasn't doing it in the constructor, but with a function that I called from the parent window, so I guess before the Loaded event was fired. But why is that ? =/ – tobiak777 Jun 18 '13 at 13:50

1 Answers1

1

Just to elaborate on my comment to give some meaning,

From MSDN Docs

Loaded entails that the logical tree that an element is contained within is complete

which helps us since the binding here has a reference to the other TextBlock element. Bindings also depend on DataContext and few other factors relative to the specific Binding but in general they get evaluated after the UI Loads for good reason.

Hence setting the Text of the source TextBlock once the UI loads results in everything working fine since Binding's are active at that point.

Viv
  • 17,170
  • 4
  • 51
  • 71