在wpf中有一个像“标签”一样的texbox?

时间:2015-04-29 12:42:15

标签: wpf

假设我有一个用户可以输入的TextBox控件,我希望文本框控件的行为与下面显示的“Tags”文本框类似,我在文本框中输入内容,我会显示建议列表,当用户选择一个建议时,我将其弹出文本框并显示一个小按钮。

我已经连接了TextBox,ListBox在弹出窗口中显示了建议,我甚至选择了有效的建议,我正在努力使按钮出现并在用户退格时消失等。

要求:我只需要一个标签。

问题:我希望按钮位于texbox内部,但现在它出现在外面。

你们有什么建议吗?

我的Xaml:

        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="30"></ColumnDefinition>
                                <ColumnDefinition Width="*"></ColumnDefinition>
                                <ColumnDefinition Width="8"></ColumnDefinition>
                                <ColumnDefinition Width="Auto"></ColumnDefinition>
                            </Grid.ColumnDefinitions>
                            <Button Content="Res" Grid.Column="0" Width="30" x:Name="tagbutton" /> <!-- This is the tag button i want to collapse show. -->
                            <TextBox x:Name="SearchTextBox" 
                                     Grid.Column="1"
                                     PreviewKeyDown="SearchTextBox_PreviewKeyDown"
                                     KeyUp="SearchTextBox_KeyUp" 
                                     Margin="0,2,0,0" FontSize="14" 
                                     Padding="3" 
                                     Text="{Binding ElementName=root, Path=SearchText, UpdateSourceTrigger=PropertyChanged, Delay=100}">                            
                            </TextBox>
 <Popup x:Name="suggestionPopup"
               PlacementTarget="{Binding ElementName=outerBorder}" 
               Placement="Bottom"                
               IsOpen="False"
               StaysOpen="False"
               AllowsTransparency="True"
               Margin="0,23,0,0">
           <MyListBoxHere>
        </Popup> 
    </Grid>

1 个答案:

答案 0 :(得分:0)

如果您希望按钮显示在TextBox控件内,则需要覆盖TextBox的控件模板以创建允许该控件的自定义控件。标准TextBox控件仅接受string(或数值)作为内容。

你可以看到按钮位于文本框内,例如:

    <Border Width="120" Height="35"
            BorderBrush="Black" BorderThickness="1">
        <StackPanel Orientation="Horizontal">
            <TextBox Width="70" BorderBrush="Transparent"/>
            <Button Content="myTag" Margin="4"/>
        </StackPanel>
    </Border>

enter image description here

然后,您可以将TextBox上的样式更改为无边框,并且与边框对象的颜色相同,然后看起来按钮位于文本框内。您的Border控件将成为“文本框”的新大纲。