繁体   English   中英

如何在wpf中使用clear按钮实现文本框?

[英]How to implement a textbox with a clear button in wpf?

我有以下UserControl 这是一个带ButtonTextBox

<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>

在Windows 7中它看起来很棒,但在Windows XP中我有以下问题:

在此输入图像描述

有关如何解决问题的任何想法? 如果我使背景透明,那么按钮没有问题,但文本位于按钮下方,看起来很奇怪。

使Button更小和/或添加一个小边距以“缩小”可见背景。

编辑:虽然看了一下(想知道这还没有被添加为一些新的功能)我发现这篇文章的分步说明,你可以尝试一下

我认为你应该在这里使用ControlTemplate ,例如你可以看到搜索文本框控件在这里你可以找到文本框模板

试试这个。

<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}" />

这是风格。

<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>

结果:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM