how to separate the background image in wpf textbo

2019-05-26 21:07发布

问题:

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>

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>

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>

回答1:

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:

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


回答2:

You can try with Dock Panel as bellow

 <DockPanel>
     <Button BorderThickness="0" DockPanel.Dock="Left" HorizontalAlignment="Right"    Height="28" Width="25" Margin="200,10,10,10">     
      </Button>
     <TextBox Text="User" FontSize="16" HorizontalAlignment="Stretch" Background="Transparent" Height="28" Width="200" Margin="-80,10,10,10"/>
   </DockPanel>

This may help you