How to make a TextBox with a Button inside in WPF?

2019-04-20 06:55发布

I'd like to make a TextBox with a Button inside, something like a DatePicker, but not exactly. Or can it be a ComboBox inside the TextBox, so you can switch the mode of the TextBox.

Can you help me?

标签: wpf xaml textbox
8条回答
Fickle 薄情
2楼-- · 2019-04-20 07:26

You may find this link helps: http://msdn.microsoft.com/en-us/library/ms752068(VS.85).aspx.

"The ControlTemplate for a TextBox must contain exactly one element that is tagged as the content host element; this element will be used to render the contents of the TextBox. To tag an element as the content host, assign it the special name PART_ContentHost. The content host element must be either a ScrollViewer or an AdornerDecorator. The content host element may not host any child elements."

查看更多
虎瘦雄心在
3楼-- · 2019-04-20 07:27

You can use RichTextBox instead of textbox and it support flowdocument in which you can place the button in it.

查看更多
ら.Afraid
4楼-- · 2019-04-20 07:27

You may use a grid to accomplish this task. The following is how I created a button which appears at the right bottom of a TextBox:

<Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <TextBox VerticalAlignment="Stretch"  HorizontalAlignment="Stretch" Grid.Row="0" />

        <Button Content="Copy" Width="40" Height="40" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="10" Grid.Row="0" />

</Grid>
查看更多
贪生不怕死
5楼-- · 2019-04-20 07:28

If you want something like a combobox or a date time picker you should create a new control, inside this new control place a text box and a button side by side inside a frame that looks like the frame of a textbox - then restyle the textbox so it doesn't have a frame.

putting a button inside a rich edit is great if you want to put a button inside a "document" but not a good substitute for a combobox.

See the ComboBox control template MSDN

查看更多
劳资没心,怎么记你
6楼-- · 2019-04-20 07:37

Just use Grid.Column same like below code

<TextBox x:Name="txtUrl" Grid.Column="1" Margin="2,2,0,2" VerticalAlignment="Center" Padding="2" PreviewKeyDown="txtUrl_PreviewKeyDown" GotFocus="txtUrl_GotFocus" PreviewMouseDown="txtUrl_PreviewMouseDown"> 

</TextBox>
<eo:BareButton x:Name="btnAddFavorite" Grid.Column=" 1"   HorizontalAlignment="Right" Style="{StaticResource WindowButtonStyle }" Margin="2" >
    <eo:BareButton.Template>
        <ControlTemplate TargetType="{x:Type eo:BareButton}">
            <Border x:Name="PART_Border" Width="22" Height="22" Background="Transparent" VerticalAlignment="Center" Margin="2,0,0,0" CornerRadius="2">
                <Path 
            HorizontalAlignment="Center"
            VerticalAlignment="Center"
                                                            Fill="Yellow" 

            Data="M 2,9 L 8,8 10,2 13,8 19,9 15,13 16,19 10,15 5,19 6,13 2,9"
        SnapsToDevicePixels="false"
            Stroke="{Binding Foreground, RelativeSource={RelativeSource AncestorType={x:Type eo:BareButton}, Mode=FindAncestor}}"
            StrokeThickness="1" />
            </Border>
            <ControlTemplate.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter TargetName="PART_Border" Property="BorderBrush" Value="#666"/>
                    <Setter TargetName="PART_Border" Property="BorderThickness" Value="1"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </eo:BareButton.Template>

</eo:BareButton>
查看更多
爱情/是我丢掉的垃圾
7楼-- · 2019-04-20 07:41

You can also use a Label and change its template to include a Button in it. To have a good overview of differences between Label and TextBlock see this post.

查看更多
登录 后发表回答