2008-12-10 31 views
8

Tôi có một ListBox để cuộn hình ảnh theo chiều ngang.WPF Xaml Tuỳ chỉnh kiểu dáng đã chọn Kiểu trong một ListBox

Tôi có XAML sau đây tôi đã sử dụng kết hợp để tạo. Nó ban đầu có một x: Key trên dòng Style TaregetType, MSDN nói để loại bỏ nó, như tôi đã nhận được lỗi trên đó. Bây giờ tôi nhận được lỗi này:

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

Tôi không hiểu làm thế nào để áp dụng tất cả các rác này theo cách đó, tôi đã thử một vài điều, không có gì đang làm việc.

Mục tiêu của tôi là để nền của mục được chọn có màu trắng, không phải màu xanh lam. Có vẻ như rất nhiều công việc cho một cái gì đó quá nhỏ!

Cảm ơn.

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

Trả lời

13

Gói Style Tag với một ItemContainerStyle như sau:

<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

Tôi đã thử các giải pháp trên nhưng nó didnt làm việc như mong đợi vì vậy tôi tìm thấy một cách khác để giải quyết vấn đề của tôi là để vô hiệu hóa lựa chọn cho listbox

đây là những gì tôi đã làm

<ListBox.ItemContainerStyle> 
    <Style TargetType="{x:Type ListBoxItem}"> 
     <Setter Property="Focusable" Value="False"/> 
    </Style> 
</ListBox.ItemContainerStyle> 
Các vấn đề liên quan