我有一个样式为“AutoCompleteTextBox”的简单文本框:
<TextBox Name="TextBox" Style="{StaticResource AutoCompleteTextBox}" />
样式位于我的资源字典中:
<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>
在我的代码隐藏文件中,我有两个吸气剂:
private Popup Popup
{
get { return Template.FindName("PART_Popup", this) as Popup; }
}
private ListBox ItemList
{
get { return Template.FindName("PART_ListBox", this) as ListBox; }
}
和文本框加载事件:
void TextBox_Loaded(object sender, RoutedEventArgs e)
{
var popup = Popup;
var itemlist = ItemList;
}
现在我有一个问题,Popup
并且ItemList
总是返回 null,为什么?