Read C# Textblock Text Property filled using Inlines

只愿长相守 提交于 2019-12-31 03:04:29

问题


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...


回答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.



来源:https://stackoverflow.com/questions/17170006/read-c-sharp-textblock-text-property-filled-using-inlines

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!