2009-10-27 11 views
6

Jak ustawić granicę domyślną dla moich obiektów ListBoxItems jako przerywaną ramkę? Zobacz następujący sposób układania go:Przerywana obramowanie w obiekcie ListBoxItem w dokumencie 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> 

Tutaj robię AlternationIndex 0, 1 i 2 do granicy przerywaną zamiast linii ciągłej. Jak to zrobić?

+0

Zobacz także moją odpowiedź tutaj: https://stackoverflow.com/a/17431518/1230982 Byliśmy w stanie użyć tej techniki, aby użyć rzeczywistej granicy i uniknąć użycia Prostokątów. –

Odpowiedz

5

Spróbuj tego:

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

Włożyłem więc prostokąt poniżej rzeczywistej granicy w szablonie sterowania. Prostokąt może mieć przerywaną linię, przerywaną lub w/e (aby zmniejszyć kreskę, wystarczy zmienić część na 2, 1 nie jest zauważalna). Tak więc domyślna grubość krawędzi prostokąta wynosi 0, ale po wybraniu ustawię grubość na 1, aby była widoczna. Zrobiłem również pewne właściwości obramowania, które mają być powiązane z rodzicem w szablonie, więc może wyglądać jak to, co ustawiłeś w swoim stylu (pędzel srebrny, grubość 0,0,0,1).