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 5986347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:39:30+00:00 2026-05-22T22:39:30+00:00

my issue is a bit similar to this one: Having an issue with my

  • 0

my issue is a bit similar to this one:
Having an issue with my TextBox control template

but I’d like to go a little further:

my issue is simple:
I have templated controls (labels, textboxes, checkboxes and so on…). Each control has default value that can all be overriden at runtime by setting the control instance’s property value. And no matter what value was set to a property, the values that I decided to use when IsEnabled = false should prevail.

now, I know that Value precedence in WPF prevents an easy implementation for this behaviour (as I learned in an other question: http://msdn.microsoft.com/en-us/library/ms743230.aspx)

but I managed to find a workaround that does the job pretty well, except for the Foreground property in a TextBox. (and only this specific case)

so, just taking care of the foreground value, here is what I do on a Label: (I got rid of all the other properties for more clarity)

    <Style TargetType="Label">
        <Setter Property="Foreground" Value="Blue"/>

        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Label">
                    <Border>
                        <ContentPresenter Name="ContentSite"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter TargetName="ContentSite" Property="TextBlock.Foreground" Value="Red"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

So basically, by default, a Label will have a Blue text, that I can override with, say, green by doing: myLabel.Foreground = Brushes.Green in code behind, and then, no matter what I set, If I write: MyLabel.IsEnabled = False, the text will become Red.

and this technique works great. (i.e.: this line in xaml will output a red text: <Label Foreground="Green" IsEnabled="False" />). It works the same way with all other controls.

but when trying to reproduce this with a textBox, it does not work at all.

here’s my code:

    <Style TargetType="TextBox">
        <Setter Property="Foreground" Value="Blue"/>

        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="TextBox">
                    <Grid>
                        <Border Name="Border">
                            <ScrollViewer x:Name="PART_ContentHost" />
                        </Border>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter TargetName="PART_ContentHost" Property="Foreground" Value="Red"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

I tried to add Foreground="{TemplateBinding Foreground}" on the PART_ContentHost with no success.

I suspect this has something to do with the fact that PART_ContentHost is a scrollviewer and there is some kind of hidden (at least from me) inheritance going on.

the only other explanation I can find is that the TextBox class has some kind of special treatment that hardcodes a link between the control itself and its PART_ContentHost part (binding or propertychanged event or something else?)

if somebody could explain this to me, this would be great!
if somebody could even provide a nice solution (one that does not involve a nasty hack like the one I’m using at the moment but will not show here so as not to cause major eye injury to the reader), this would be even greater!

  • 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-22T22:39:30+00:00Added an answer on May 22, 2026 at 10:39 pm

    By default, that ScrollViewer is itself templated to look like this:

    <ControlTemplate x:Key="ScrollViewerControlTemplate1" TargetType="{x:Type ScrollViewer}">
        <Grid x:Name="Grid" Background="{TemplateBinding Background}">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="Auto"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <Rectangle x:Name="Corner" Grid.Column="1" Fill="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" Grid.Row="1"/>
            <ScrollContentPresenter x:Name="PART_ScrollContentPresenter" CanContentScroll="{TemplateBinding CanContentScroll}" CanHorizontallyScroll="False" CanVerticallyScroll="False" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Grid.Column="0" Margin="{TemplateBinding Padding}" Grid.Row="0"/>
            <ScrollBar x:Name="PART_VerticalScrollBar" AutomationProperties.AutomationId="VerticalScrollBar" Cursor="Arrow" Grid.Column="1" Maximum="{TemplateBinding ScrollableHeight}" Minimum="0" Grid.Row="0" Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" Value="{Binding VerticalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" ViewportSize="{TemplateBinding ViewportHeight}"/>
            <ScrollBar x:Name="PART_HorizontalScrollBar" AutomationProperties.AutomationId="HorizontalScrollBar" Cursor="Arrow" Grid.Column="0" Maximum="{TemplateBinding ScrollableWidth}" Minimum="0" Orientation="Horizontal" Grid.Row="1" Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}" Value="{Binding HorizontalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" ViewportSize="{TemplateBinding ViewportWidth}"/>
        </Grid>
    </ControlTemplate>
    

    I got that out of the TextBox by using Blend, by the way.

    I’m betting that if you add your triggers to that ScrollContentPresenter in that template the way you did it to the ContentPresenter in the Label that it’ll work for you.

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

Sidebar

Related Questions

I've seen some similar posts, but nothing quite like this... I have a website
I am having a bit of a issue. I have an NSMutableDictionary with 10
I am having a bit of a strange issue. I have a ListView which
I have a situation similar to this one struct Child { u16 x, y;
I have a small part in my code thats similar to this one (ofcourse
I have made a question which is a bit similar to this, though the
I am having a little bit of trouble making the table look like I
Being a bit perplexed about this issue by now, I hope some of you
I have a bit of an issue with my SQL statement. It is a
I'm having a bit of an issue uploading files to a database. I'll post

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.