none
请问:怎样动态更改AutoCompleteBox的样式,比如边框颜色,背景等? RRS feed

答案

  • 你好,

    可以通过重写模板做到.比如下面的代码我把模板的TextBox的背景色绑到AutoCompleteBox的背景色属性.代码里只要改变AutoCompleteBox1.Background 就可以了.

    <

     

    UserControl xmlns:controls="clr-namespace:Microsoft.Windows.Controls;assembly=Microsoft.Windows.Controls" x:Class="SilverlightApplication12.Page"

     

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

     

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

     

    xmlns:coreLib="clr-namespace:System;assembly=mscorlib"

     

    Width="1400" Height="1300" xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows">

     

     

    <UserControl.Resources>

     

     

    <ControlTemplate x:Key="Template1" TargetType="controls:AutoCompleteBox">

     

     

    <Grid Margin="{TemplateBinding Padding}" Background="{TemplateBinding Background}">

     

     

    <vsm:VisualStateManager.VisualStateGroups>

     

     

    <vsm:VisualStateGroup x:Name="PopupStates">

     

     

    <vsm:VisualStateGroup.Transitions>

     

     

    <vsm:VisualTransition GeneratedDuration="0:0:0.1" To="PopupOpened"/>

     

     

    <vsm:VisualTransition GeneratedDuration="0:0:0.2" To="PopupClosed"/>

     

     

    </vsm:VisualStateGroup.Transitions>

     

     

    <vsm:VisualState x:Name="PopupOpened">

     

     

    <Storyboard>

     

     

    <DoubleAnimation Storyboard.TargetName="PopupBorder" Storyboard.TargetProperty="Opacity" To="1.0"/>

     

     

    </Storyboard>

     

     

    </vsm:VisualState>

     

     

    <vsm:VisualState x:Name="PopupClosed">

     

     

    <Storyboard>

     

     

    <DoubleAnimation Storyboard.TargetName="PopupBorder" Storyboard.TargetProperty="Opacity" To="0.0"/>

     

     

    </Storyboard>

     

     

    </vsm:VisualState>

     

     

    </vsm:VisualStateGroup>

     

     

    </vsm:VisualStateManager.VisualStateGroups>

     

     

    <TextBox Background="{TemplateBinding Background}" Margin="0" x:Name="Text" Style="{TemplateBinding TextBoxStyle}" IsTabStop="True"/>

     

     

    <Popup x:Name="Popup">

     

     

    <Border HorizontalAlignment="Stretch" x:Name="PopupBorder" Opacity="0.0" Background="#11000000" BorderThickness="0" CornerRadius="3">

     

     

    <Border.RenderTransform>

     

     

    <TranslateTransform X="1" Y="1"/>

     

     

    </Border.RenderTransform>

     

     

    <Border HorizontalAlignment="Stretch" Opacity="1.0" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="3" Padding="0">

     

     

    <Border.RenderTransform>

     

     

    <TransformGroup>

     

     

    <TranslateTransform X="-1" Y="-1"/>

     

     

    </TransformGroup>

     

     

    </Border.RenderTransform>

     

     

    <Border.Background>

     

     

    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">

     

     

    <GradientStop Color="#FFDDDDDD" Offset="0"/>

     

     

    <GradientStop Color="#AADDDDDD" Offset="1"/>

     

     

    </LinearGradientBrush>

     

     

    </Border.Background>

     

     

    <ListBox x:Name="SelectionAdapter" ItemContainerStyle="{TemplateBinding ItemContainerStyle}" ItemTemplate="{TemplateBinding ItemTemplate}" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto"/>

     

     

    </Border>

     

     

    </Border>

     

     

    </Popup>

     

     

    </Grid>

     

     

    </ControlTemplate>

     

     

     

    </UserControl.Resources>

     

     

     

    <Grid x:Name="LayoutRoot" Background="Red">

     

     

    <StackPanel>

     

     

    <controls:AutoCompleteBox x:Name="AutoCompleteBox1" Template="{StaticResource Template1}" Width="200" Height="30" />

     

     

    </StackPanel>

     

     

    </Grid>

    </

     

    UserControl>



    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    2009年6月22日 9:37
    版主