I am working on a control for my WPF application in which I want a Button inside a TextBox . Button will contain an image that changes with the mouse, but I already know how to make this part. I'm having problems with what Button actually includes in a TextBox so that it takes up as much space as it is transparent. Below is the inscription that I have tried so far:
XAML:
<Grid> <TextBox x:Name="SearchBoxView" HorizontalAlignment="Right" Width="200" Margin="5, 5, 10, 5" FontSize="16" KeyUp="SearchBoxKeyDown" Text="{Binding SearchText, Mode=TwoWay}" Grid.Column="0"/> <Button Background="Transparent" HorizontalAlignment="Right" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"> <Button.Content> <Image Source="Image.png" Stretch="None" RenderOptions.BitmapScalingMode="NearestNeighbor" VerticalAlignment="Center" HorizontalAlignment="Center"/> </Button.Content> </Button> </Grid>
I followed this question: How to implement a text box with a transparent button in wpf? , which is also related to this article: WPF Search Text Box . The XAML suggested in the question did not work, and I think this is because of the Style used, which I do not have access to. The article provided too much information, mainly talking about the properties of triggers and dependencies needed to replace the search and delete the mouse icons. So, I ask for help finding a simple solution on how to do this. Thanks!
EDIT: I followed the advice on answers, and I can draw the button correctly, but it wonβt appear in the text box, and if that happens, the text will still work under it. I included XAML and a photo of what is happening:
XAML:
<Grid Margin="5, 5, 10, 5"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> <ColumnDefinition Width="Auto"/> </Grid.ColumnDefinitions> <TextBox x:Name="SearchBoxView" HorizontalAlignment="Right" Width="200" FontSize="16" KeyUp="SearchBoxKeyDown" Text="{Binding SearchText, Mode=TwoWay}" Grid.Column="0"/> <Button Background="{Binding Backgound, ElementName=SearchBoxView}" HorizontalAlignment="Right" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Click="SearchBoxViewButtonClick" Grid.Column="1"> <Button.Template> <ControlTemplate> <Border HorizontalAlignment="Center" VerticalAlignment="Center"> <Image Source="Image.png" Width="12" Height="12"/> </Border> </ControlTemplate> </Button.Template> </Button> </Grid>
Picture

source share