Share via

TextBox Displays Wrong Color

ChrisLeow 1 Reputation point
2022-07-31T17:12:43.613+00:00

Hi. Recently I have been encountering a very painful problem. I am developing under .Net Framework 4.5. I have a custom-styled TextBox, whose foreground is bound to a property in the ViewModel called IsValid. Besides there is a converter which converts the TextBox's foreground color to Red if IsValid is False, Black if IsValid is True. The ViewModel implemented the INotifyPropertyChanged interface and of course calls OnPropertyChanged each time it is modified.

Here comes the problem. The initial foreground color of the TextBox's text is Red, which means the IsValid property is True. Then I change the Visibility of the TextBox to collapsed, IsValid to False meanwhile. Next, I change the Visibility to Visible and IsValid to be False. The TextBox shows up but the foregroud color is Black! Notice the TextBox is now in invalid state in my scenario. I use Snoop tool to snoop the Structure of my application. Snoop tells me that the foreground color is Red. But what I see is that the text color is black.

I have no clue what causes this. Under most circumstances the whole scheme works fine, but sometimes it doesn't and this is a reproducible problem in my application. I wonder if there is a way that I could dive into the control's visual tree to see what is actually happing inside the TextBox at runtime. Otherwise I cannot come up with some good ideas to investigate the problem. Any help is appreciated.

226522-wechatimg556.png

Developer technologies | Windows Presentation Foundation

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.