0

I've create a custom control which inherit from WPF TextBox.

My Control Template simply add a small button on textbox in order to delete its text quicker.

However, I've noticed that when my textBox got the focus, its border doesn't change (blue color) as is the case for classic textbox.

I would preserver all aspect of original textBox, just like the border when the control get focus.

Am I missing something?

@@EDIT

<TextBox x:Class="XTextBox.WKTextBox"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         Height="23" Width="200"
         >



<TextBox.Resources>
    <ControlTemplate x:Key="IconButton" TargetType="{x:Type ToggleButton}">
        <Border>
            <ContentPresenter />
        </Border>
    </ControlTemplate>
</TextBox.Resources>

<TextBox.Style>

    <Style TargetType="TextBox">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TextBox}">
                    <Grid>
                        <Border BorderThickness="1" BorderBrush="DarkGray">
                            <ScrollViewer x:Name="PART_ContentHost" />
                        </Border>
                        <ToggleButton Template="{StaticResource IconButton}"
                          MaxHeight="21" 
                          Margin="-1,0,0,0" 
                          Name="imgButton" 
                          Focusable="False"
                          IsChecked="False">

                            <Image Name="imgClearText" Source="Images\x.png" Stretch="Uniform" Opacity="0.5" Visibility="Visible" HorizontalAlignment="Right"  >
                            </Image>

                        </ToggleButton>

                    </Grid>

                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

</TextBox.Style>

Jürgen Steinblock
  • 30,746
  • 24
  • 119
  • 189
Bit79
  • 3
  • 4
  • please add some details and show your modified code to make it easier to understand what's going wrong. – chris-sc Sep 07 '15 at 10:44
  • You've added a grid with a border that is set to DarkGray, wouldn't that hide the contained edit box border? – o_weisman Sep 07 '15 at 11:44
  • No. Border setted to DarkGray doesn't seem be the problem. if I remove the border I'm unable to see my textbox. – Bit79 Sep 07 '15 at 12:16

2 Answers2

1

Unfortunately, you can't simply Replace part of default template in WPF without loosing functionallity. I believe the easiest solution would be to donwload Blend (it comes with VS2015). Open it, create an emty textbox and edit its template:

enter image description here

Blend will make a copy of default template, so you won't loose any of your default behaviour, like selection, focus etc. Then you can save a project, open it in VS and refactor it as you want. Like moving style to dictionary or something.

Community
  • 1
  • 1
netaholic
  • 1,345
  • 10
  • 22
0

You can manually get the same effect by adding handlers for the GotFocus & LostFocus events of the Border and set the highlight colors you want there.

<Border BorderThickness="1" BorderBrush="DarkGray" LostFocus="Border_LostFocus" GotFocus="Border_GotFocus">

and in your .cs file

private void Border_LostFocus(object sender, RoutedEventArgs e)
{
  ((Border)sender).BorderBrush = new SolidColorBrush(Colors.DarkGray);
}

private void Border_GotFocus(object sender, RoutedEventArgs e)
{
  ((Border)sender).BorderBrush = new SolidColorBrush(Colors.LightBlue);
}
PaulF
  • 6,673
  • 2
  • 18
  • 29
  • It's a workaround however I cannot the same behavior, neither the same colors of classic textBox – Bit79 Sep 07 '15 at 12:29
  • 1
    I've noticed that on Mouse enter the border is lightBlue, but on focused event the blue color seems to be a little more dark. Ho can I get that color? – Bit79 Sep 07 '15 at 12:36
  • I Can't see that effect on my PC – PaulF Sep 07 '15 at 12:41