Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4599234
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T23:30:42+00:00 2026-05-21T23:30:42+00:00

I have a requirement to allow selecting the text displayed in read only screens.

  • 0

I have a requirement to allow selecting the text displayed in read only screens.

A simple solution one of our developers came up with is using a TextBox instead of a Label or TextBlock, with the following style:

<Style x:Key="ControlData" TargetType="{x:Type TextBox}">
    <Setter Property="Background" Value="Transparent" />
    <Setter Property="BorderThickness" Value="0" />
    <Setter Property="IsReadOnly" Value="True" />
    <Setter Property="TextWrapping" Value="Wrap" />
    <!-- unrelated properties ommitted -->
</Style>

I don’t like the idea of using a TextBox there, among other things because it forces me to use Binding Mode=OneWay for read-only properties, so I was trying to define a Style that I can apply to a label to get the same result:

<Style x:Key="SelectableLabel" TargetType="Label">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Label">
                <TextBox Style="{StaticResource ControlData}"
                         Text="{Binding Path=Content, Mode=OneWay,
                                RelativeSource={RelativeSource FindAncestor,
                                                AncestorType=Label}}"/>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

The problem is, some of my bindings have StringFormat set, and that is lost.

  • Is there a way to keep the formatting of the outer binding?
  • Should I create my template/binding differently?
  • Is there a whole different approach that’s better than this?
  • Should I stop with the nitpicking and go with the TextBox?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T23:30:43+00:00Added an answer on May 21, 2026 at 11:30 pm

    I think it’s a good idea to use a Label and override the control template. At the end this is what the separation of Control logic and layout in WPF is for and you should make use of it to have a cleaner, more understandable xaml code.

    Did you try using a TemplateBinding instad of normal Binding? I’m not sure if this will preserve the outer binding, but it’s the recommended Binding to use in DataTemplates.

    You should also take a look at the ContentPresenter class, although I’m not sure if it’s applicable here, since your inner text box only has a Text property of type string…

    Edit:
    I solved the problem, although it was more complicated then I thought…

    Create the style as following:

    <Application.Resources>
        <LabelTest:ContentToTextConverter x:Key="contentConverter"  />
        <Style TargetType="Label">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Label">
                        <TextBox DataContext="{Binding Mode=OneWay, RelativeSource={RelativeSource FindAncestor,AncestorType=Label}}">
                            <TextBox.Text>
                                <MultiBinding Converter="{StaticResource contentConverter}" >
                                    <Binding Mode="OneWay" Path="ContentStringFormat" />
                                    <Binding Mode="OneWay" Path="Content" />
                                </MultiBinding>
                            </TextBox.Text>
                        </TextBox>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Application.Resources>
    

    The ContentToTextConverter is defined as following:

    public class ContentToTextConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            var format = values[0] as string;
    
            if (string.IsNullOrEmpty(format)) format = "{0}";
            else if(format.IndexOf('{') < 0) format = "{0:" + format + "}";
    
            return string.Format(culture, format, values[1]);
        }
    
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }
    

    Example of how to use:

    <Label ContentStringFormat="{}{0}$">
         <System:Int64>15</System:Int64>
    </Label>
    

    It would be nice to find a nicer solution than this, but for the moment it should work as expected.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a requirement where i need to allow only numbers in text box.
I have a requirement to allow users to browse directories on our file server,
We have a simple requirement - to allow drawing on top of a picture
One of our product requirement is to allow the generation of Search form. So
I have a requirement to allow users to open word document from web page
I have a requirement to create a simple windows forms application that allows an
In our project we have requirement that, after receiving sms message from third party
I have a requirement to allow or prevent modification to an ObservableCollection<T> (or at
I have a requirement to allow users in a content management system to create
I have the requirement to allow users to view the content of a document

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.