解决方法
您可以通过自定义ListBox项目的现有控制模板来执行此操作.简单的方法是启动Expression Blend,右键单击ListBoxItem,转到Edit Control Parts(模板),然后选择Edit a copy …然后根据需要自定义fillColor和fillColor2矩形的填充颜色.
下面的Xaml将ListBoxItem鼠标悬停颜色设置为透明,所选颜色为绿色,但您可以根据需要自定义此颜色:
<UserControl x:Class="SilverlightApplication2.Page" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows" Width="400" Height="300" Background="#FF000000"> <UserControl.Resources> <Style x:Key="ListBoxItemStyleTransparent" 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}"> <vsm:visualstatemanager.VisualStateGroups> <vsm:VisualStateGroup x:Name="CommonStates"> <vsm:VisualState x:Name="normal"/> <vsm:VisualState x:Name="MouSEOver"> <Storyboard> <DoubleAnimationUsingKeyFrames Storyboard.TargetName="fillColor" Storyboard.TargetProperty="Opacity"> <SplineDoubleKeyFrame KeyTime="0" Value=".35"/> </DoubleAnimationUsingKeyFrames> </Storyboard> </vsm:VisualState> <vsm:VisualState x:Name="disabled"> <Storyboard> <DoubleAnimationUsingKeyFrames Storyboard.TargetName="contentPresenter" Storyboard.TargetProperty="Opacity"> <SplineDoubleKeyFrame KeyTime="0" Value=".55"/> </DoubleAnimationUsingKeyFrames> </Storyboard> </vsm:VisualState> </vsm:VisualStateGroup> <vsm:VisualStateGroup x:Name="SelectionStates"> <vsm:VisualState x:Name="Unselected"/> <vsm:VisualState x:Name="Selected"> <Storyboard> <DoubleAnimationUsingKeyFrames Storyboard.TargetName="fillColor2" Storyboard.TargetProperty="Opacity"> <SplineDoubleKeyFrame KeyTime="0" Value=".75"/> </DoubleAnimationUsingKeyFrames> </Storyboard> </vsm:VisualState> </vsm:VisualStateGroup> <vsm:VisualStateGroup x:Name="Focusstates"> <vsm:VisualState x:Name="Focused"> <Storyboard> <ObjectAnimationUsingKeyFrames Duration="0" Storyboard.TargetName="FocusVisualElement" Storyboard.TargetProperty="Visibility"> <discreteObjectKeyFrame KeyTime="0"> <discreteObjectKeyFrame.Value> <Visibility>Visible</Visibility> </discreteObjectKeyFrame.Value> </discreteObjectKeyFrame> </ObjectAnimationUsingKeyFrames> </Storyboard> </vsm:VisualState> <vsm:VisualState x:Name="Unfocused"/> </vsm:VisualStateGroup> </vsm:visualstatemanager.VisualStateGroups> <Rectangle x:Name="fillColor" IsHitTestVisible="False" Opacity="0" RadiusX="1" RadiusY="1" Fill="Transparent"/> <Rectangle x:Name="fillColor2" IsHitTestVisible="False" Opacity="0" Fill="#FF00FF00" RadiusX="1" RadiusY="1"/> <ContentPresenter HorizontalAlignment="Left" Margin="{TemplateBinding Padding}" x:Name="contentPresenter" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}"/> <Rectangle x:Name="FocusVisualElement" Visibility="Collapsed" stroke="#FF6DBDD1" strokeThickness="1" RadiusX="1" RadiusY="1"/> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> </UserControl.Resources> <Grid x:Name="LayoutRoot" Background="White"> <ListBox x:Name="ListBoxTest"> <ListBoxItem Content="Some String" Style="{StaticResource ListBoxItemStyleTransparent}" /> </ListBox> </Grid> </UserControl>
fillColor Rectangle指定当用户鼠标悬停在ListBoxItem上时使用的颜色.在上面的代码中,我将其设置为Transparent,当您将鼠标悬停在ListBoxItem上时,不会出现任何颜色.
fillColor2指定当选择ListBoxItem时要使用的颜色.在上面的代码中,我指定了#FF00FF00,所以当ListBoxItem被选中时,颜色将变绿.
在您的情况下,您可以将fillColor2 Rectangle的“填充”属性设置为“透明”,以便在用户选择项目时模拟没有颜色.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。