How to implement a textbox with a clear button in wpf?
Asked Answered
C

3

12

I have the following UserControl. It's a TextBox with a Button:

<Grid>
    <TextBox
        Grid.Column="0"
        Text="{Binding Text, 
               RelativeSource={RelativeSource AncestorType=UserControl}, 
               UpdateSourceTrigger=PropertyChanged}"
         x:Name="TextBox" />

     <Button
         Background="{Binding Background, ElementName=TextBox}"
         Grid.Column="1"
         Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}"
         HorizontalAlignment="Right"
         Visibility="{Binding IsClearButtonVisible,
                      RelativeSource={RelativeSource AncestorType=UserControl},
                      Converter={StaticResource BooleanToVisibilityConverter}}"
         Command="{Binding ClearTextCommand,
                   RelativeSource={RelativeSource AncestorType=UserControl}}"    
         HorizontalContentAlignment="Center"
         VerticalContentAlignment="Center" >

         <Button.Content>
             <Image
                 Source="{StaticResource Delete2}"
                 Stretch="None"
                 RenderOptions.BitmapScalingMode="NearestNeighbor"
                 VerticalAlignment="Center"
                 HorizontalAlignment="Center" />
        </Button.Content>
    </Button>
</Grid>

In Windows 7 it looks great but in Windows XP I have the following issue:

enter image description here

Any ideas on how to fix the issue? If I make the background transparent then there is no problem with the button but the text gets below the button and looks weird.

Chicle answered 29/11, 2012 at 11:19 Comment(0)
S
6

Make the Button smaller and/or add a small margin to "downsize" the visible background.

Edit: While looking around a bit (wondering that this hasn't been added as some new Feature) I've found this article with step-by-step instructions you could give a try.

Superdreadnought answered 29/11, 2012 at 11:24 Comment(2)
If I do that then on W7 the button is smaller than the textbox and when you hoover / click on it looks ugly too :(Chicle
Try the blog post I've linked.Superdreadnought
A
6

Try this.

<TextBox x:Name="SearchFilter" VerticalAlignment="Center" Width="200"  
                                 Text="{Binding SearchItemString}" />
<Button Margin="-20,10,5,0" Width="25" Height="25" Content="X" 
                        Style="{StaticResource TransparentStyle}" />

Here is the Style.

<Style x:Key="TransparentStyle" TargetType="{x:Type Button}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border Background="Transparent">
                    <ContentPresenter/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Result:

Adder answered 6/11, 2018 at 10:38 Comment(1)
Pretty quick and dirty... thanks. You could even add right-padding to the TextBox equal to the amount of the button width so your text doesn't go underneath the button.Caliban
S
3

I think you should use ControlTemplates here, as example you can see Search Text Box Control and here you can find textbox template

Stunner answered 29/11, 2012 at 12:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.