0

I'm writing an application where the user has to click "edit" in a few particular views to be able to edit them, I've solved this by binding the controllers (textboxes, comboboxes etc) IsEnabled to my "NotReadOnly" property in the VM.

Now my users want to be able to copy data from my controllers (in particular, the textboxes) without having to click my edit button first. This is not possible since IsEnabled=false disables most functionality.

Changing to "IsReadOnly = True" is not an alternative, I want the look and feel of a disabled controller (background, font changes etc) so that my users can clearly see it's not in edit mode, and I don't want to do all of that with bindings to my "ReadOnly" property in the VM, there are also cases where more than one background property determines wether some controller is enabled or not.

So I hope to find some way of getting copy (and preferably also selecting/scrolling) working in disabled controllers.

If that's not possible, is there any way of getting the look and feel of a disabled controller without having to add a ton of XAML to every single controller?

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Mårten
  • 231
  • 2
  • 14

2 Answers2

3

It is not possible to select text from disabled textbox. What you can do is make it read only and set the similar to disabled.

<TextBox IsEnabled="False">Disabled</TextBox>
<TextBox IsReadOnly="True" Text="Readonly" Background="LightGray" Foreground="Gray"></TextBox>

see this post: How to change disabled background color of TextBox in WPF

Community
  • 1
  • 1
lerner1225
  • 862
  • 7
  • 25
1

You do not to have to add XAML to each window where there are your controls. Just add this code to App.Xaml file of your WPF project and all your textbox controls in your application will have the same behavior for IsEnabled=false:

<SolidColorBrush x:Key="DisabledForegroundBrush" Color="Red" />
        <SolidColorBrush x:Key="DisabledBackgroundBrush" Color="White" />
        <Style TargetType="TextBox">
            <Setter Property="Background" Value="White"/>
            <Setter Property="BorderBrush" Value="Black"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="TextBox">
                        <Border Name="Bd" BorderThickness="{TemplateBinding BorderThickness}" 
                                             BorderBrush="{TemplateBinding BorderBrush}" 
                                             Background="{TemplateBinding Background}" 
                                             SnapsToDevicePixels="true">
                            <ScrollViewer Name="PART_ContentHost" Background="{TemplateBinding Background}" 
                                          SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsReadOnly" Value="True">
                                <Setter Value="{StaticResource DisabledBackgroundBrush}" Property="Background" />
                                <Setter Value="{StaticResource DisabledForegroundBrush}" Property="Foreground" />
                                <Setter TargetName="PART_ContentHost" Property="Background" Value="Blue"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

If you want your styles to be used all over the application, across different windows, you can define it for the entire application:

<Application x:Class="WpfApplication.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfApplication"
             StartupUri="MainWindow.xaml">
    <Application.Resources>

        <SolidColorBrush x:Key="DisabledForegroundBrush" Color="Red" />
        <SolidColorBrush x:Key="DisabledBackgroundBrush" Color="White" />
        <Style TargetType="TextBox">
            <!--The code omitted for the brevity-->
            </Setter>
        </Style>

    </Application.Resources>
</Application>

Read this superior tutorial about Styles

StepUp
  • 36,391
  • 15
  • 88
  • 148
  • This seems promising, but I guess you meant Trigger Property="IsReadOnly" since that's where I want to apply the style widely if I want both the ability to select/copy and disabled look/feel? Thanks! – Mårten Nov 13 '15 at 12:40
  • @Mårten sure, you're right. I've updated my code in answer. If my reply is answer for your question, you can assign as an answer to help other people searching for similar question. – StepUp Nov 13 '15 at 12:43
  • Sure, I'll just try it out a bit more to see if it works as I intended. – Mårten Nov 13 '15 at 12:57
  • Let me know if you have trouble with this and I can try to help further. – StepUp Nov 13 '15 at 13:00