如何编辑AutoCompleteBox中的弹出样式

本文关键字:样式 AutoCompleteBox 何编辑 编辑 | 更新日期: 2023-09-27 18:18:26

如何在Windows Phone 8, xaml, c#中更改AutoCompleteBox弹出框的样式(背景和边框)?

如何编辑AutoCompleteBox中的弹出样式

您必须编辑控件模板,因为默认模板中的这些属性是不可样式化的。您可能想要创建一个新样式,或者您可以只是覆盖现有的样式。以下是默认样式(在工具包源代码Themes/Generic.xaml下):

<Style TargetType="controls:AutoCompleteBox">
    <Setter Property="Background" Value="{StaticResource PhoneTextBoxBrush}"/>
    <Setter Property="BorderBrush" Value="{StaticResource PhoneTextBoxBrush}"/>
    <Setter Property="BorderThickness" Value="{StaticResource PhoneBorderThickness}"/>
    <Setter Property="FontFamily" Value="{StaticResource PhoneFontFamilyNormal}"/>
    <Setter Property="FontSize" Value="{StaticResource PhoneFontSizeMediumLarge}"/>
    <Setter Property="Foreground" Value="{StaticResource PhoneTextBoxForegroundBrush}"/>
    <Setter Property="ItemTemplate">
        <Setter.Value>
            <DataTemplate>
                <ContentControl Content="{Binding}" Margin="8,7"/>
            </DataTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="Padding" Value="6,0,6,4"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="controls:AutoCompleteBox">
                <Grid>
                    <TextBox
                        x:Name="Text"
                        Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}"
                        FontFamily="{TemplateBinding FontFamily}"
                        FontSize="{TemplateBinding FontSize}"
                        FontStyle="{TemplateBinding FontStyle}"
                        FontWeight="{TemplateBinding FontWeight}"
                        Foreground="{TemplateBinding Foreground}"
                        InputScope="{TemplateBinding InputScope}"
                        Opacity="{TemplateBinding Opacity}"
                        Padding="{TemplateBinding Padding}"
                        Style="{TemplateBinding TextBoxStyle}"/>
                    <Popup x:Name="Popup">
                        <ListBox
                            x:Name="Selector"
                            Background="White"
                            BorderBrush="{StaticResource PhoneTextBoxEditBorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            FontFamily="{TemplateBinding FontFamily}"
                            FontSize="{TemplateBinding FontSize}"
                            FontStyle="{TemplateBinding FontStyle}"
                            FontWeight="{TemplateBinding FontWeight}"
                            Foreground="{TemplateBinding Foreground}"
                            IsTabStop="False"
                            ItemContainerStyle="{TemplateBinding ItemContainerStyle}"
                            ItemTemplate="{TemplateBinding ItemTemplate}"
                            Opacity="{TemplateBinding Opacity}"
                            Padding="0,8"/>
                    </Popup>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

所以你可以在弹出框中硬编码一个新颜色,例如:

                        <ListBox
                            x:Name="Selector"
                            Background="Black"
                            BorderBrush="Red"

或者,你可以使边框/背景样式化,但与TextBox的颜色相同:

                        <ListBox
                            x:Name="Selector"
                            Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"

这样,应用于控件实例的样式将同时应用于文本框和弹出框:

<controls:AutoCompleteBox Background="Black" BorderBrush="Red" ... />