2008-12-10 23 views
8

Tengo un ListBox que desplaza las imágenes horizontalmente.WPF Xaml estilo personalizado Estilo de elemento seleccionado en un ListBox

Tengo el siguiente XAML que utilicé blend para crearlo. Originalmente tenía una x: Key en la línea Style TaregetType, dijo MSDN para eliminarlo, ya que estaba recibiendo errores en eso. Ahora estoy recibiendo este error:

Error 3 Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead.

No entiendo cómo aplicar toda esta basura de esa manera, lo he intentado varias cosas, nada está funcionando.

Mi objetivo es que el fondo del elemento seleccionado sea blanco, no azul. ¡Parece mucho trabajo para algo tan pequeño!

Gracias.

<ListBox ItemsSource="{Binding Source={StaticResource WPFApparelCollection}}" 
     Grid.Row="1" Margin="2,26,2,104" ScrollViewer.VerticalScrollBarVisibility="Hidden" 
      ScrollViewer.HorizontalScrollBarVisibility="Hidden" SelectionMode="Single" 
     x:Name="list1" MouseLeave="List1_MouseLeave" MouseMove="List1_MouseMove" Style="{DynamicResource ListBoxStyle1}" > 
     <Style TargetType="{x:Type ListBoxItem}"> 
      <Setter Property="Background" Value="Transparent"/> 
      <Setter Property="HorizontalContentAlignment" Value="{Binding Path=HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/> 
      <Setter Property="VerticalContentAlignment" Value="{Binding Path=VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/> 
      <Setter Property="Padding" Value="2,0,0,0"/> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="{x:Type ListBoxItem}"> 
         <Border x:Name="Bd" SnapsToDevicePixels="true" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}"> 
          <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> 
         </Border> 
         <ControlTemplate.Triggers> 
          <Trigger Property="IsSelected" Value="true"> 
           <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/> 
           <Setter Property="Background" TargetName="Bd" Value="#FFFFFFFF"/> 
          </Trigger> 
          <MultiTrigger> 
           <MultiTrigger.Conditions> 
            <Condition Property="IsSelected" Value="true"/> 
            <Condition Property="Selector.IsSelectionActive" Value="false"/> 
           </MultiTrigger.Conditions> 
           <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/> 
           <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/> 
          </MultiTrigger> 
          <Trigger Property="IsEnabled" Value="false"> 
           <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> 
          </Trigger> 
         </ControlTemplate.Triggers> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
     <ListBox.ItemsPanel> 
      <ItemsPanelTemplate> 
       <VirtualizingStackPanel 
      Orientation="Horizontal" 
      IsItemsHost="True" /> 
      </ItemsPanelTemplate> 
     </ListBox.ItemsPanel> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <Image Source="{Binding Image}" /> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 

Respuesta

13

Wrap la etiqueta de estilo con un ItemContainerStyle de la siguiente manera:

<ListBox ItemsSource="{Binding Source={StaticResource WPFApparelCollection}}" 
     Grid.Row="1" Margin="2,26,2,104" 
     ScrollViewer.VerticalScrollBarVisibility="Hidden" 
     ScrollViewer.HorizontalScrollBarVisibility="Hidden" 
     SelectionMode="Single" 
     x:Name="list1" MouseLeave="List1_MouseLeave" MouseMove="List1_MouseMove" 
     Style="{DynamicResource ListBoxStyle1}" > 

     <ListBox.ItemContainerStyle> 
      <Style TargetType="{x:Type ListBoxItem}"> 
       <Setter Property="Background" Value="Transparent"/> 
      </Style> 
<!-- the rest of your code, but close the ItemContainerStyle --> 
     </ListBox.ItemContainerStyle> 
    </ListBox> 
2

Probé la solución anterior, pero que aún no ha dado los resultados previstos por lo que he encontrado otra manera de resolver mi problema, que es deshabilitar la selección de cuadro de lista

esto es lo que hice

<ListBox.ItemContainerStyle> 
    <Style TargetType="{x:Type ListBoxItem}"> 
     <Setter Property="Focusable" Value="False"/> 
    </Style> 
</ListBox.ItemContainerStyle> 
Cuestiones relacionadas