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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:21:15+00:00 2026-06-05T17:21:15+00:00

I have re-styled a textbox using a control template. The styling works well but

  • 0

I have re-styled a textbox using a control template. The styling works well but I cannot now get the text value from the textbox programatically after it has been changed in the textbox.

Here is how I use the style:

     <TextBox
        x:Name="KernTextBox"
        Style="{StaticResource MediumTextBoxStyle}"
        TextWrapping="Wrap"
        MinLines="1"
        MaxLines="5"
        VerticalScrollBarVisibility="Auto"
        TextChanged="TextChanged"
        />

Here is what does not work.

  get { return KernTextBox.Text; }

Here is the style:

<Style x:Key="MediumTextBoxStyle" TargetType="TextBox">
    <Setter Property="FontSize" Value="18" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="TextBox">
                <Grid>

                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Normal"/>
                            <VisualState x:Name="MouseOver" />
                            <VisualState x:Name="ReadOnly">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames
                                        Storyboard.TargetName="ButtonRectangle"
                                        Storyboard.TargetProperty="Fill">
                                        <DiscreteObjectKeyFrame
                                            KeyTime="0"
                                            Value="{StaticResource ForegroundBrush}" />
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames
                                        Storyboard.TargetName="ButtonText"
                                        Storyboard.TargetProperty="Foreground">
                                        <DiscreteObjectKeyFrame
                                            KeyTime="0"
                                            Value="{StaticResource BackgroundBrush}" />
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames
                                        Storyboard.TargetName="ButtonText"
                                        Storyboard.TargetProperty="Background">
                                        <DiscreteObjectKeyFrame
                                            KeyTime="0"
                                            Value="{StaticResource ForegroundBrush}" />
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames
                                        Storyboard.TargetName="ButtonText"
                                        Storyboard.TargetProperty="BorderBrush">
                                        <DiscreteObjectKeyFrame
                                            KeyTime="0"
                                            Value="{StaticResource ForegroundBrush}" />
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Disabled">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames
                                        Storyboard.TargetName="ButtonRectangle"
                                        Storyboard.TargetProperty="Fill">
                                        <DiscreteObjectKeyFrame
                                            KeyTime="0"
                                            Value="{StaticResource ForegroundBrush}" />
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames
                                        Storyboard.TargetName="ButtonText"
                                        Storyboard.TargetProperty="Foreground">
                                        <DiscreteObjectKeyFrame
                                            KeyTime="0"
                                            Value="{StaticResource BackgroundBrush}" />
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames
                                        Storyboard.TargetName="ButtonText"
                                        Storyboard.TargetProperty="Background">
                                        <DiscreteObjectKeyFrame
                                            KeyTime="0"
                                            Value="{StaticResource ForegroundBrush}" />
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames
                                        Storyboard.TargetName="ButtonText"
                                        Storyboard.TargetProperty="BorderBrush">
                                        <DiscreteObjectKeyFrame
                                            KeyTime="0"
                                            Value="{StaticResource ForegroundBrush}" />
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>

                        </VisualStateGroup>

                        <VisualStateGroup x:Name="CheckStates">
                            <VisualState x:Name="Checked" />
                            <VisualState x:Name="Unchecked"/>
                        </VisualStateGroup>

                        <VisualStateGroup x:Name="FocusStates">
                            <VisualState x:Name="Focused" />
                            <VisualState x:Name="Unfocused"/>
                        </VisualStateGroup>

                    </VisualStateManager.VisualStateGroups>

                    <Rectangle 
                        x:Name="ButtonRectangle" 
                        Stroke="Transparent" 
                        UseLayoutRounding="False" 
                        Fill="{StaticResource BackgroundBrush}">
                    </Rectangle>

                    <TextBox 
                        x:Name="ButtonText" 
                        Text="{Binding Path=Text, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
                        FontSize="{TemplateBinding FontSize}"
                        HorizontalAlignment="Stretch" 
                        VerticalAlignment="Center" 
                        Margin="2 0"
                        TextWrapping="{TemplateBinding TextWrapping}"
                        MinLines="{TemplateBinding MinLines}"
                        MaxLines="{TemplateBinding MaxLines}"
                        VerticalScrollBarVisibility="{TemplateBinding VerticalScrollBarVisibility}"
                        Padding="{TemplateBinding Padding}"
                        Foreground="{StaticResource ForegroundBrush}"
                        Background="{StaticResource BackgroundBrush}"
                        >
                    </TextBox>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

EDIT:

Final working style:

<Style 
    x:Key="TextBoxStyle" 
    TargetType="{x:Type TextBox}">
    <Setter
        Property="Foreground"
        Value="{StaticResource ForegroundBrush}" />
    <Setter
        Property="FontSize"
        Value="18" />
    <Setter 
        Property="CaretBrush"
        Value="{StaticResource ForegroundBrush}" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TextBox}">
                <Border 
                    Name="Border"
                    Background="{StaticResource BackgroundBrush}"
                    BorderBrush="{StaticResource ForegroundBrush}"
                    BorderThickness="1" >
                    <ScrollViewer Margin="0" x:Name="PART_ContentHost"/>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter TargetName="Border" Property="Background" Value="{StaticResource ForegroundBrush}"/>
                        <Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource BackgroundBrush}"/>
                        <Setter Property="Foreground" Value="{StaticResource BackgroundBrush}"/>
                    </Trigger>
                    <Trigger Property="IsReadOnly" Value="True">
                        <Setter TargetName="Border" Property="Background" Value="{StaticResource ForegroundBrush}"/>
                        <Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource BackgroundBrush}"/>
                        <Setter Property="Foreground" Value="{StaticResource BackgroundBrush}"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
  • 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-06-05T17:21:17+00:00Added an answer on June 5, 2026 at 5:21 pm

    Is there a reason why you place a textbox inside a textbox? Checkout the original style and template of a textbox and modify it from there. My guess is, the textbox uses a TextBoxView(hidden in the Scrollviewer) internally and the Text property is not bound via TemplateBinding, but calculated between these internal classes. So maybe thats the reason why you can’t bind your text property to the textbox text in your control template.

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

Sidebar

Related Questions

I have created autocomplete textbox using jquery, but am getting the ORA: 00933 SQL
I'm using Microsoft Windows Phone 7 Tools Beta. I have a custom control that
I'm using a TextBox control and want the characters to turn red after a
I have an ASP.NET text control FromDate whose visible property is set to false,
I have a control template defined, call it myVal, that is used for validation
I have some what complex UI requirement, need help in understanding type of control/style/template
I am using this home-made function to validate text fields, but for some reason,
I have a WPF application, and I am attempting to style a TextBox using
I have a hidden TextBox control on my page that updates a span tag
I was tring to create a simple datepicker control using JQuery, but i was

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.