2009-10-27 15 views
6

¿Cómo puedo hacer que el borde predeterminado en ListBoxItems sea un borde punteado? Ver siguiente forma de peinar que:Borde punteado en ListBoxItem en WPF

<Grid.Resources> 
    <Style TargetType="{x:Type ListBoxItem}"> 
     <Setter Property="Height" Value="30" /> 
     <Setter Property="BorderThickness" Value="0,0,0,1" /> 
     <Setter Property="BorderBrush" Value="Silver" /> 
     <Setter Property="Content" Value="" /> 
     <Style.Triggers> 
      <Trigger Property="ItemsControl.AlternationIndex" Value="3"> 
       <Setter Property="BorderBrush" Value="Black"></Setter> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 
</Grid.Resources> 

Aquí hago la AlternationIndex 0, 1 y 2 a un borde de puntos en lugar de una línea continua. ¿Cómo puede hacerse esto?

+0

También, vea mi respuesta aquí: https://stackoverflow.com/a/17431518/1230982 Pudimos utilizar esta técnica para usar un borde real y evitar el uso de rectángulos. –

Respuesta

5

Prueba esto:

<Window.Resources> 
     <SolidColorBrush x:Key="SelectedBackgroundBrush" Color="#DDD" /> 
     <SolidColorBrush x:Key="DisabledForegroundBrush" Color="#888" /> 
     <SolidColorBrush x:Key="DisabledBackgroundBrush" Color="#EEE" /> 
     <!-- SimpleStyles: ListBoxItem --> 
     <Style TargetType="ListBoxItem" x:Key="ListBoxItemTemplate"> 
      <Setter Property="SnapsToDevicePixels" Value="true"/> 
      <Setter Property="OverridesDefaultStyle" Value="true"/> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="ListBoxItem"> 
         <Grid> 
          <Rectangle x:Name="Rectangle" Fill="Transparent" Stroke="Black" 
             StrokeDashCap="Square" StrokeThickness="0" SnapsToDevicePixels="True"> 
           <Rectangle.StrokeDashArray> 
            <sys:Double>5</sys:Double> 
           </Rectangle.StrokeDashArray> 
          </Rectangle> 
          <Border 
           Name="Border" 
           Padding="2" 
           BorderThickness="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=BorderThickness}" 
           BorderBrush="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=BorderBrush}"> 
           <ContentPresenter /> 
          </Border> 
         </Grid> 
         <ControlTemplate.Triggers> 
          <Trigger Property="IsSelected" Value="true"> 
           <Setter TargetName="Rectangle" Property="StrokeThickness" Value="1" /> 
           <Setter TargetName="Border" Property="BorderThickness" Value="0" /> 
          </Trigger> 
          <Trigger Property="IsSelected" Value="true"> 
           <Setter TargetName="Rectangle" Property="Fill" Value="{StaticResource SelectedBackgroundBrush}" /> 
          </Trigger> 
          <Trigger Property="IsEnabled" Value="false"> 
           <Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}" /> 
          </Trigger> 
         </ControlTemplate.Triggers> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 

     <Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource ListBoxItemTemplate}"> 
      <Setter Property="Height" Value="30" /> 
      <Setter Property="BorderThickness" Value="0,0,0,1" /> 
      <Setter Property="BorderBrush" Value="Silver" /> 
      <Style.Triggers> 
       <Trigger Property="ItemsControl.AlternationIndex" Value="3"> 
        <Setter Property="BorderBrush" Value="Black"></Setter> 
       </Trigger> 
      </Style.Triggers> 
     </Style> 

    </Window.Resources> 
    <StackPanel> 
     <ListBox> 
      <ListBoxItem Content="Item 1" /> 
      <ListBoxItem Content="Item 2" /> 
      <ListBoxItem Content="Item 3" /> 
      <ListBoxItem Content="Item 4" /> 
     </ListBox> 
    </StackPanel> 

por lo que poner un rectángulo abajo de la frontera real en la plantilla de control. El rectángulo puede tener su borde punteado o discontinuo o w/e (para hacer que el tablero sea más pequeño simplemente cambie la parte a 2, 1 no se nota). Por lo tanto, el valor predeterminado del grosor del borde del rectángulo es 0, pero cuando se selecciona, establezco el grosor en 1 para que esté visible. Hice algunas propiedades de borde para enlazar a su elemento primario con plantilla también, por lo que puede parecerse a lo que establece en su estilo (cepillo de plata, espesor 0,0,0,1).

Cuestiones relacionadas