Search code examples
c#wpfisenabledreadonly-attribute

Disabled controls but with ability to select and copy


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?


Solution

  • 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