如何在WPF中为TextBox添加“清除”按钮?

6
我正在开发一个WPF应用程序的控件,其中我希望在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>

图片:

带按钮的搜索框


目前的 XAML 中按钮位于网格中,但你没有任何网格行。你是真的需要将按钮放在文本框中,还是只放在控件中? - paparazzo
@Blam 如果您查看我对ArsenMkrt答案的评论,您会看到我需要什么,或者至少认为我需要什么。 - Sean Cogan
好的,但我建议您将问题更改为控件中的按钮,如果您不需要它在文本框中。 - paparazzo
1个回答

5

指定 Button 模板,而不是内容

<Button>
    <Button.Template>
        <ControlTemplate>
            <Border HorizontalAlignment="Center" VerticalAlignment="Center" >
                <Image Source="pack://application:,,,/Organizr;component/DataLayer/Images/ActiveDeleteIcon.png" 
                               Width="16" 
                               Height="16"/>
            </Border>
        </ControlTemplate>
     </Button.Template>
</Button>

编辑

顺便提一句,在您的情况下,图像将覆盖TextBox和输入的文本。我建议将这些控件放置在网格的不同列中,如下所示。

<Grid>
   <Grid.ColumnDefinitions>
      <ColumnDefinition />
      <ColumnDefinition Width="Auto"/>
   </Grid.ColumnDefinitions>
   <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
            Grid.Column="1"
            Background="Transparent"
            HorizontalAlignment="Right"
            HorizontalContentAlignment="Center" VerticalContentAlignment="Center">
           <ControlTemplate>
                <Border HorizontalAlignment="Center" VerticalAlignment="Center" >
                    <Image Source="pack://application:,,,/Organizr;component/DataLayer/Images/ActiveDeleteIcon.png" 
                                   Width="16" 
                                   Height="16"/>
                </Border>
            </ControlTemplate>
         </Button.Template>
    </Button>

这个答案非常好用,但现在我遇到了一个问题,文本会消失在按钮后面,因为按钮只是简单地放置在文本框上方。有没有办法可以将按钮实际放在文本框中?我意识到这与我的原始问题不同,但我认为这才是我真正需要的。非常感谢你迄今为止的帮助。 - Sean Cogan
是的,在网格的不同列中放置文本框和按钮。将第二列的宽度设置为自动,第一列保留默认或设置为*。如果您想要覆盖文本框边框,可以给按钮一些小的负边距。 - Arsen Mkrtchyan
我把文本框和按钮放在网格的不同列中,但是按钮只显示了一半。于是我调整了按钮的边距,当我成功让它显示在文本框中时,文本框的文本仍然会出现在按钮下面。我希望文本能够停在按钮的位置。 - Sean Cogan
糟糕,我才意识到我实际上没有将按钮放在第二列...抱歉。 - Sean Cogan
1
你有为按钮设置 Grid.Column 吗? - Arsen Mkrtchyan
好的,我已经意识到如何解决我的问题了。我只需要将 TextBox 的 Grid.ColumnSpan 设置为 2,这样它就会在按钮下面。然后,为了防止实际文本进入按钮下面,我设置了 TextBox 的填充。现在一切都像我想要的那样工作了。非常感谢您的帮助! - Sean Cogan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接