我正在开发一个WPF应用程序的控件,其中我希望在
我跟进了这个问题:如何在WPF中实现带清除按钮的文本框?,其中还链接到了这篇文章:WPF搜索文本框。问题中建议使用的XAML没有起作用,我认为这是因为他们使用的
TextBox
内放置一个Button
。该Button
将包含一个鼠标悬停时更改的图像,但我已经知道如何做到这一点。我遇到的问题是实际上将Button
包含在TextBox
中,使其仅占用与图像相同的空间并且是透明的。以下是我尝试过的标记:
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>
我跟进了这个问题:如何在WPF中实现带清除按钮的文本框?,其中还链接到了这篇文章:WPF搜索文本框。问题中建议使用的XAML没有起作用,我认为这是因为他们使用的
Style
,而我无法访问。该文章提供了太多信息,主要讲解了需要交换搜索和删除图标的触发器和依赖属性。因此,我请求帮助找到一个简单的解决方案来实现这一点。谢谢!
编辑:我按照答案的建议进行了跟进,我能够正确地设置按钮样式,但它仍然不会显示在文本框中,如果显示了,文本仍然会在其下面。我包括了XAML和正在发生的情况的图片:
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>
图片: