Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

In my project, I have a window in which it has a text box and a button. I am using WPF with MVVM Pattern as suggested in different sites to have a better maintainability and unit testing.

On LostFocus event of the text box I want to change the border brush property of that text box.

How do you suggest me to implement this?

Thanks
Posted
Comments
Sergey Alexandrovich Kryukov 18-Mar-13 12:00pm    
This is simple, but I wonder how did you work with you UI so far. Never used any of the properties? (Because Border is no different from all other UI elements in this respect.)
—SA
Rahul Krishnan R 18-Mar-13 23:06pm    
I am using MVVM Pattern and I am totally a beginner in this.

1 solution

If you have this border in XAML, give it a Name. This will expose it in the code of your window class, as a class instance member. Then you can use the properties of you border in your code:
http://msdn.microsoft.com/en-us/library/system.windows.controls.border.borderbrush.aspx[^].

See also: http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.name.aspx[^].

—SA
 
Share this answer
 
Comments
Rahul Krishnan R 18-Mar-13 23:07pm    
This will violate the MVVM rules by having code behind the view. I want this to be achieved in the viewmodel class of the view.
Sergey Alexandrovich Kryukov 18-Mar-13 23:50pm    
Who told you so, MVVM? :-)
If you want to pervert the nature of programming, do it. This is my personal opinion, and you should have your own head on your shoulders.

I am pretty well familiar with different architectural patterns, but their authors can not dictate me any final solutions. I seriously think that XAML (good thing by itself) and programming with designer is heavily abused, lead to bulk of manual brainless non-reusable low-quality code. Right approach is to put as little functionality to XAML as possible.

You may not trust me, and this is natural and good, because the truth becomes truth only when you prove or follow the proof personally, by yourself. Try to become more than a beginner and then use your own brain to analyze your experience. I personally don't trust any of those development "theories" until I try to validate them by myself. If I could see mathematically strong theoretical reasoning, it would be something serious (like in theory of computation, cryptography, etc.), but so far, all these development "theories" to my is no more than blah-blahology. How much longer developers will allow fooling themselves.

At the same time, ideas of MVVM are very fruitful; we just should not take fanatically all which is written, use our own brains instead.

Sooner or later, this exaggerated XAML overuse will go down. The practical question for you is: what kind of code are you going behind before it happens? It will depend on one factor: how much thinking are you going to put in architecture and design.

Good luck,
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900