Silverlight自訂漂亮的ListBox

來源:互聯網
上載者:User

第一個話說看起來效果不錯,不過就是滑鼠點擊的時候無法改變顏色,

第二個雖然樣式有些差,但是可以把第一個的樣式放到下面去就OK了。

具體效果看這裡

http://bbs.csdn.net/topics/390328614

這個是自己曾經遇到的問題,非常感謝2位大神的協助,下面是2個源碼,直接貼上去就可以運行

 

  <UserControl.Resources>    <Style x:Key="ListBoxItemStyle1" TargetType="ListBoxItem">    <Setter Property="Padding" Value="3"/>    <Setter Property="HorizontalContentAlignment" Value="Left"/>    <Setter Property="VerticalContentAlignment" Value="Top"/>    <Setter Property="Background" Value="Transparent"/>    <Setter Property="BorderThickness" Value="1"/>    <Setter Property="TabNavigation" Value="Local"/>    <Setter Property="Template">    <Setter.Value>    <ControlTemplate TargetType="ListBoxItem">    <Grid Background="{TemplateBinding Background}">    <VisualStateManager.VisualStateGroups>    <VisualStateGroup x:Name="CommonStates">    <VisualState x:Name="Normal"/>    <VisualState x:Name="MouseOver">    <Storyboard>    <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="rectangle">    <EasingColorKeyFrame KeyTime="0" Value="#FF90322A"/>    <EasingColorKeyFrame KeyTime="0:0:0.4" Value="White"/>    </ColorAnimationUsingKeyFrames>    </Storyboard>    </VisualState>    <VisualState x:Name="Disabled"/>    </VisualStateGroup>    <VisualStateGroup x:Name="SelectionStates">    <VisualState x:Name="Unselected"/>    <VisualState x:Name="Selected"/>    </VisualStateGroup>    <VisualStateGroup x:Name="FocusStates">    <VisualState x:Name="Focused"/>    <VisualState x:Name="Unfocused"/>    </VisualStateGroup>    </VisualStateManager.VisualStateGroups>    <Rectangle x:Name="rectangle" Width="75" Height="25" RadiusX="5" RadiusY="5" Stroke="Black" Margin="52,0,69,0">    <Rectangle.Fill>    <LinearGradientBrush StartPoint="1,0">    <GradientStop Color="#BD5E54" Offset="0.0"/>    <GradientStop Color="#90322A" Offset="0.9"/>    </LinearGradientBrush>    </Rectangle.Fill>    </Rectangle>    <TextBlock Text="{Binding FunctionName}" HorizontalAlignment="Center"/>    </Grid>    </ControlTemplate>    </Setter.Value>    </Setter>    </Style>    </UserControl.Resources>    <Grid x:Name="LayoutRoot" Background="White">        <ListBox HorizontalAlignment="Left" Height="100" VerticalAlignment="Top" Width="200" ItemContainerStyle="{StaticResource ListBoxItemStyle1}"  >            <ListBoxItem Content="ListBoxItem"/>            <ListBoxItem Content="ListBoxItem"/>            <ListBoxItem Content="ListBoxItem"/>            <ListBoxItem Content="ListBoxItem"/>        </ListBox>        <Button Content="Button" HorizontalAlignment="Center" VerticalAlignment="Center" Width="75"  Visibility="Collapsed"/>    </Grid></UserControl>

 

 

 

第二個

<UserControl.Resources><Style x:Key="ListBoxItemStyle1" TargetType="ListBoxItem"><Setter Property="Padding" Value="3"/><Setter Property="HorizontalContentAlignment" Value="Left"/><Setter Property="VerticalContentAlignment" Value="Top"/><Setter Property="Background" Value="Transparent"/><Setter Property="BorderThickness" Value="1"/><Setter Property="TabNavigation" Value="Local"/><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="ListBoxItem"><Grid Background="{TemplateBinding Background}"><VisualStateManager.VisualStateGroups><VisualStateGroup x:Name="CommonStates"><VisualState x:Name="Normal"/><VisualState x:Name="MouseOver"><Storyboard><DoubleAnimation Duration="0" To=".35" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="fillColor"/></Storyboard></VisualState><VisualState x:Name="Disabled"><Storyboard><DoubleAnimation Duration="0" To=".55" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="contentPresenter"/></Storyboard></VisualState></VisualStateGroup><VisualStateGroup x:Name="SelectionStates"><VisualState x:Name="Unselected"/><VisualState x:Name="Selected"><Storyboard><DoubleAnimation Duration="0" To=".75" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="fillColor2"/></Storyboard></VisualState></VisualStateGroup><VisualStateGroup x:Name="FocusStates"><VisualState x:Name="Focused"><Storyboard><ObjectAnimationUsingKeyFrames Duration="0" Storyboard.TargetProperty="Visibility" Storyboard.TargetName="FocusVisualElement"><DiscreteObjectKeyFrame KeyTime="0"><DiscreteObjectKeyFrame.Value><Visibility>Visible</Visibility></DiscreteObjectKeyFrame.Value></DiscreteObjectKeyFrame></ObjectAnimationUsingKeyFrames></Storyboard></VisualState><VisualState x:Name="Unfocused"/></VisualStateGroup></VisualStateManager.VisualStateGroups><!--以下兩句是關鍵!!--><Rectangle x:Name="fillColor" Fill="#FFff0000" IsHitTestVisible="False" Opacity="0" RadiusY="1" RadiusX="1"/><Rectangle x:Name="fillColor2" Fill="#FF00ff00" IsHitTestVisible="False" Opacity="0" RadiusY="1" RadiusX="1"/><ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}"/><Rectangle x:Name="FocusVisualElement" RadiusY="1" RadiusX="1" Stroke="#FF6DBDD1" StrokeThickness="1" Visibility="Collapsed"/></Grid></ControlTemplate></Setter.Value></Setter></Style></UserControl.Resources>

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.