WPF Xaml自定义样式列表框中的选定项样式

时间:2008-12-10 04:30:47

标签: wpf listbox styles selecteditem

我有ListBox水平滚动图像。

我有以下XAML我用blend来创建它。它最初在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>

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:2)

我尝试了上面的解决方案,但它没有按预期工作,所以我找到另一种方法来解决我的问题,即禁用列表框的选择

这就是我做的事情

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