Take the 2-minute tour ×

In my WPF application, my Viewmodel has a boolean property IsOwnerOf and a string property Title. If IsOwner==false, I want a TextBlock displaying the Title (because if you're not the owner, you should not be able to edit it) and if IsOwner==true, I want a TextBox displaying Title - obviously at the same place in the view.

Also I don't want to do it codebehind since I follow the MVVM pattern. Thought about Style.Triggers, but with them I can only influence attributes of an element, not the element type itself, or can I?

EDIT: Practically the answers below regarding triggering Visibility or IsReadOnly work, but I still would like to see a conceptually better answer! What if I replace the TextBox resp. TextBlock by elements that don't have these convenient properties? There must be a better way than creating both and hiding one of them, that just doesn't sound right...

share|improve this question
Have you just considered a TextBox and bind the ReadOnly property to !IsOwnerOf msdn.microsoft.com/en-us/library/… –  Blam Mar 11 '13 at 13:44
You can bind the visibility of your controls to the boolean property. Here's an article to help you rhyous.com/2011/02/22/binding-visibility-to-a-bool-value-in-wpf –  Rafał Saltarski Mar 11 '13 at 13:45
Please post your markup and the view model. Also, please read this article to help you understand why there are certain pieces to a question that need to exist (msmvps.com/blogs/jon_skeet/archive/2010/08/29/…). –  Michael Perrenoud Mar 11 '13 at 13:46
Hmm, sure, I understand, but all is standard here, so I though I omit the code... Also, to the fisrt two comments, see my comments to the answers below. –  ToaoG Mar 11 '13 at 13:49
add comment

2 Answers

up vote 1 down vote accepted

The easiest option is to always drop a TextBox and bind it's IsEnabled or IsReadOnly property to the IsOwner flag.

You can also use a DataTemplateSelector to achieve this.

share|improve this answer
Sure, but a TextBox looks different, it "suggests editability" to the user, whereas a TextBlock not. –  ToaoG Mar 11 '13 at 13:48
Thanks! The DatatemplateSelector is the solution I was looking for, i.e., I make two templates, one containing the TextBlock, the other one the TextBox, and then I choose the template according to the value of the flag. –  ToaoG Mar 12 '13 at 9:52
@ToaoG You're welcome. I wanted to improve the answer and give a sample code for the template selector approach, but I didn't found the time. I'm gald you solved it. –  MD.Unicorn Mar 12 '13 at 10:03
add comment

You can use triggers to change the Visibility of your TextBlock and TextBox using a BooleanToVisibilityConverter

share|improve this answer
So you suggest to always have both present, and just juggle the visibility depending on the value of IsOwnerOf? That seems like a waste of resources to me, because I create more objects than I need, and hence not the best practice. Or is that irrelevant? –  ToaoG Mar 11 '13 at 13:47
@ToaoG, I would say that's irrelevant. You're talking about two objects - and keep in mind that a TextBlock doesn't even have a window handle. –  Michael Perrenoud Mar 11 '13 at 13:59
Ok, granted it is of minimal concern here, but the question was also of broader interest, i.e., what should I do in a similar situation, where I want to toggle between different elements depending on the value of a flag, which dont necessarily have a readonly or visibility property? What is the cleanest way? This one seems like ad hoc problem solving, sry. –  ToaoG Mar 11 '13 at 14:13
@ToaoG Ad hoc? Visibility is a property of UIElement. msdn.microsoft.com/en-us/library/system.windows.uielement.aspx –  Blam Mar 11 '13 at 18:15
add comment

Your Answer


