I have a simple TextBox with the Style “AutoCompleteTextBox”:
<TextBox Name="TextBox" Style="{StaticResource AutoCompleteTextBox}" />
The Style is located in my Resource Dictionary:
<Style x:Key="AutoCompleteTextBox" TargetType="{x:Type TextBox}">
<Setter Property="KeyboardNavigation.TabNavigation" Value="None" />
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
<Setter Property="AllowDrop" Value="true" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Grid>
<Border x:Name="Bd" SnapsToDevicePixels="true">
<ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</Border>
<Popup x:Name="PART_Popup" StaysOpen="False" Placement="Bottom" IsOpen="False">
<ListBox x:Name="PART_ListBox" HorizontalContentAlignment="Stretch"/>
</Popup>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
In my Code Behind File I have two getters:
private Popup Popup
{
get { return Template.FindName("PART_Popup", this) as Popup; }
}
private ListBox ItemList
{
get { return Template.FindName("PART_ListBox", this) as ListBox; }
}
and the textbox load event:
void TextBox_Loaded(object sender, RoutedEventArgs e)
{
var popup = Popup;
var itemlist = ItemList;
}
Now I have the problem that Popup and ItemList returns always null, why?
In XAML you create a normal TextBox and style it, in code behind you have
this.Template&this, how is that supposed to work? There is no way thatthisis actually theTextBox.