2008-12-10 74 views
8

我有一個ListBox水平滾動圖像。WPF Xaml自定義樣式列表框中選定的項目樣式

我有以下XAML我用混合來創建它。它最初在Style TaregetType系列中有一個x:Key,MSDN表示將其刪除,因爲我在這方面遇到了錯誤。現在,我得到這個錯誤:

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

我不明白如何應用這一切的垃圾呀,我已經試過幾件事情的,沒有什麼工作。

我的目標是讓選定的項目背景爲白色,而不是藍色。對於這麼小的東西似乎很多工作!

謝謝。

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

回答

13

裹有如下的ItemContainerStyle的風格標籤:

<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

我嘗試了上述解決方案,但它並沒有想象中那麼努力,我發現另一種方式來解決我的問題,就是要禁用選擇的列表框

這是我做過什麼

<ListBox.ItemContainerStyle> 
    <Style TargetType="{x:Type ListBoxItem}"> 
     <Setter Property="Focusable" Value="False"/> 
    </Style> 
</ListBox.ItemContainerStyle> 
相關問題