2009-02-10 7 views
15

Tengo un cuadro de lista de Silverlight y quiero eliminar el resalte de cambio de color que se produce cuando el usuario selecciona un elemento en el cuadro de lista.Silverlight - Detener destacados en un cuadro de lista

De forma predeterminada, cuando se selecciona un elemento, se resalta el elemento como una especie de color azul claro.

¿Cómo puedo evitar que esto ocurra?

Como una pregunta complementaria, ¿cómo puedo personalizar esto a cualquier color arbitrario?

Gracias.

Respuesta

20

Puede hacerlo personalizando la Plantilla de control existente para un Elemento ListBox. La forma más fácil de hacerlo es abrir Expression Blend, hacer clic con el botón derecho en ListBoxItem, ir a Editar partes de control (Plantilla) y seleccionar Editar una copia ... luego personalizar el color de relleno de los rectángulos fillColor y fillColor2 según sea necesario.

el XAML siguiente establece el color ListBoxItem ratón por encima de ser transparente y el color seleccionado para ser de color verde brillante, pero esto se puede personalizar a sus necesidades:

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

El fillColor rectángulo especifica el color que desea utilizar cuando el usuario mouses-sobre un ListBoxItem. En el código de arriba, he configurado esto para que sea Transparente para que no aparezca ningún color al pasar el mouse por encima de ListBoxItem.

El fillColor2 especifica el color que se utilizará cuando se seleccione un ListBoxItem. En el código anterior he especificado # FF00FF00 para que el color sea verde brillante cuando se selecciona un ListBoxItem.

En su situación, establecería la propiedad Rellenar del rectángulo fillColor2 en Transparente para simular que no hay color cuando el usuario selecciona un elemento.

+0

Esta es una muy buena respuesta, desafortunadamente tengo que configurar el estilo programáticamente como lo pueblan el cuadro de lista con todo tipo de artículos diferentes. Tal vez necesito usar algunos datos para la creación de plantillas hrmm ... – JSmyth

+0

Tu respuesta funcionó conmigo al agregar myListItem.Style = (Style) this.Resources ["ListBoxItemStyleTransparent"]; Marcará su respuesta como la respuesta aceptada :) – JSmyth

+1

Sí, así es como hace referencia al estilo programáticamente. Si está utilizando el Estilo en varios archivos .xaml, otra cosa a considerar es declararlo en en App.xaml. A continuación, puede hacer referencia a través de Application.Current.Resources ["ListBoxItemStyleTransparent"]. –

-1
<ListBox.ItemContainerStyle> 
    <Style TargetType="ListBoxItem"> 
     <Setter Property="Focusable" Value="false"></Setter> 
    </Style> 
</ListBox.ItemContainerStyle> 
+2

"Focusable no es una propiedad de ListBoxItem" –

Cuestiones relacionadas