如何在AutoCompleteBox中编辑弹出窗口的样式

时间:2014-08-17 14:53:30

标签: c# xaml windows-phone-8

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

1 个答案:

答案 0 :(得分:1)

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

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

所以你可以在Popup中为ListBox硬编码一种新颜色,例如:

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

或者,您可以设置边框/背景样式,但颜色与TextBox相同:

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

这样,应用于控件实例的样式将同时应用于TextBox和Popup:

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