views:

63

answers:

1

How can i solve the following (simplified) problem? M-V-VM context. I want to show text at the UI. In case the user has the rights to change the text, i want to use a textbox to manipulate the text. In case the user has no rights, i want to use a label to only show the text. My main problem: how to exchange textbox and label and bind Text resp. Content to the same property in viewmodel.

Thanks for your answers Toni

A: 

There are a few ways of achieving this, with varying degrees of ease of reuse. You can have a DataTemplateSelector that could return the appropriate DataTemplate for a given property (depending on how this is written, you may be able to use it for each of your properties).

You could create a DataTemplate for each property, and change visibility based on a DataTrigger (this gets really annoying, as it is a lot of copy and paste).

I think the easiest way of doing this is with a specialized ControlTemplate for the TextBox. Basically, when it is disabled, instead of graying it out, you can just make it look like a TextBlock:

<ControlTemplate x:Key="PermissionedTextBox" TargetType="{x:Type TextBox}">
    <Border x:Name="bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}">
        <ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" />
    </Border>
    <ControlTemplate.Triggers>
        <Trigger Property="IsEnabled" Value="False">
            <Setter TargetName="bd" Property="BorderBrush" Value="{x:Null}" />
            <Setter TargetName="bd" Property="Background" Value="{x:Null}" />
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

Then you can use it like so:

<TextBox Text="{Binding PermissionedText}" IsEnabled="{Binding CanEdit}" />
Abe Heidebrecht
Hi Abe, thanks a lot for your suggestion.Sometimes the simplest way is the best way. I use your ControlTemplate and all works as expected.Toni
Scott Olson