I am attempting to attach a "Browse" button to a number of textboxes that will contain filenames.
I was aiming for something like this:
<TextBox Text="c:\Filename.txt" Template="{StaticResource FileBrowser}"/>
The control template I declared is as follows:
<Window.Resources>
<ControlTemplate x:Key="FileBrowser" TargetType="{x:Type TextBox}">
<Grid Grid.Row="{TemplateBinding Grid.Row}" Grid.Column="{TemplateBinding Grid.Column}">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<ContentPresenter Grid.Column="0" Grid.Row="0"/>
<Button Grid.Column="1" Content="..."/>
</Grid>
</ControlTemplate>
</Window.Resources>
But when I attempt to use this, there is no-longer a TextBox there. Only a blank space, followed by "..."
Does anyone know what I'm doing wrong? Is this the sort of thing that can only be resolved via making a new UserControl?
TextBox defines a TemplatePart named "PART_ContentHost" of type ScrollViewer. All of the text handling in the code relies on this element existing in the template. Since you haven't included it in your template the TextBox is essentially broken. Change your ContentPresenter to this instead:
<ScrollViewer x:Name="PART_ContentHost" Grid.Column="0" Grid.Row="0"/>