wpfsystemcolors

Retrieve the Default Selection Opacity


Text selection in WPF (text boxes etc.) looks different from that of other UI toolkits, as WPF uses a semi-transparent overlay over the text for its selection rectangle.

The SystemColors class provides the color used for that rectangle in the HighlightColor property (and the other properties related to it). However, that color is fully opaque. Hence, the color has to be used as a reduced opacity.

Documentation pages on TextBoxBase.SelectionOpacity, PasswordBox.SelectionOpacity and on FlowDocumentPageViewer.SelectionOpacity suggest:

The default is 0.4.

On the other hand, this blogpost says:

SelectionOpacity (...) defaults to a value of .6.

Whatever that default value is, is there any way to retrieve the selection opacity to be used with SystemColors.HighlightColor from the environment, rather than hardcoding an arbitrary value (which may not even be the best choice for non-standard highlight colors)?


Solution

  • No, and yes.. There is no SystemColors.HighlightOpacity - but there should be.

    The SelectionOpacity default property value is hardcoded to 0.4 in WPF: http://referencesource.microsoft.com/#PresentationFramework/Framework/System/Windows/Controls/Primitives/TextBoxBase.cs,9716201433499854

    I don't exactly know how this can differ(0.6 as you said), it seems to be hardcoded as 0.4 in every .NET that supports it(4.0, 4.5).

    If you want to actually query the default value of SelectionOpacityProperty, you can do so:

    var prop = TextBoxBase.SelectionOpacityProperty;
    var defValue = (double) prop.DefaultMetadata.DefaultValue; // 0.4