how to separate the background image in wpf textbox?


I am new to WPF. In my project i have tried user name textbox box with usericon.

My XAML Code:-

> <TextBox Height="30" Width="180" Margin="67,123,256,158">
>             <TextBox.Background>
>                 <ImageBrush  ImageSource="Images/User.png" Stretch="None" AlignmentX="Left" AlignmentY="Center" />
>             </TextBox.Background>
>         </TextBox>

enter image description here

But when i type the user name, text-box cursor starts from the left end, two or three letters are not visible and its looking ugly. (see the image) How i can separate this background image in textbox.

Edit 1-

>  <Canvas>
>         <DockPanel Canvas.Left="115" Canvas.Top="64">
>             <Button BorderThickness="0" DockPanel.Dock="Left" HorizontalAlignment="Right" Height="28" Width="23">
>                 <Button.Background>
>                     <ImageBrush  ImageSource="Images/User.png" Stretch="None" AlignmentX="Left" AlignmentY="Center" />
>                 </Button.Background>
>             </Button>
>             <TextBox Height="28" Width="115" HorizontalAlignment="Stretch" Background="Transparent"/>
>         </DockPanel>
>         </Canvas>

enter image description here

I have tried as per the suggestion. But the problem is the button and text box are shows separate. It not look like user icon in texbox. And when i move the cursor to button, the usericon has invisible. help me to improve my code..

EDIT 2 With Answer:-

Add the below code in grid resource for Button Hover Effect Disable.

<Style x:Key="ButtonWithoutHover" TargetType="Button">
                <Setter Property="OverridesDefaultStyle" Value="True"/>
                <Setter Property="Margin" Value="0"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="Button">
                            <Border Name="border" 
                            BorderThickness="0"                                                        
                            Background="{TemplateBinding Background}">
                                <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                            </Border>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsMouseOver" Value="True">
                                    <Setter TargetName="border" Property="BorderBrush" Value="Black" />
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>     

And Add this to wpf window XAML

<Canvas >
            <Border BorderThickness="2" BorderBrush="DarkBlue" Canvas.Left="307" Canvas.Top="65">
            <DockPanel Canvas.Left="115" Canvas.Top="30">
                <Button Style="{StaticResource ButtonWithoutHover}" BorderThickness="0" DockPanel.Dock="Left" HorizontalAlignment="Right" Height="28" Width="23">
                    <Button.Background>
                        <ImageBrush  ImageSource="Images/User.png" Stretch="None" AlignmentX="Left" AlignmentY="Center" />
                    </Button.Background>
                </Button>
                <TextBox FontSize="20"  BorderThickness="0" Height="28" Width="115" HorizontalAlignment="Stretch" Background="Transparent"/>
            </DockPanel>
            </Border>           
        </Canvas>

enter image description here


Answers:


You could take a look at Asryael's answer on this question. It solves a similar issue without having to edit any templates. Asryael's solution does require some extra styling.

You could also try the following, it simply adds some padding:

Preview

<TextBox Padding="16,2" Width="115" Height="28">
    <TextBox.Background>
        <ImageBrush ImageSource="Images/User.png" Stretch="None" AlignmentX="Left"/>
    </TextBox.Background>
</TextBox>