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

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


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


I created a textbox control and added thisIt seems to work, but not the ideal situation cos it recreates another textbox.

<TextBox.Template> <ControlTemplate>        <Grid>            <Grid.ColumnDefinitions></Grid.ColumnDefinitions>            <TextBox Grid.Column="0"></TextBox>            <Button HorizontalAlignment="Right" Width="25" Grid.Column="1">            </Button>        </Grid>             </ControlTemplate></TextBox.Template>


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