帮助为ListBoxItem创建样式

时间:2009-03-17 15:22:17

标签: wpf formatting listboxitem

我是样式的新手,需要帮助为ListBoxItem创建一个样式,该样式将为项目提供透明背景,然后在悬停时使字体变为黄金。单击时不应更改颜色,鼠标移开时应恢复正常。它仍然必须将所选对象传递给ListBox的PreviewMouseRightButtonDown事件

我目前使用REUXABLES THEMES中的默认字典,但它可以为应用程序的这部分显示添加很多颜色。

谢谢,

    <DataTemplate x:Key="ItemsTemplate">
        <StackPanel Orientation="Vertical"
              Margin="0,5,0,5"
              Width="{Binding 
              Path=ActualWidth,
              RelativeSource={RelativeSource 
              Mode=FindAncestor, 
              AncestorType={x:Type ScrollContentPresenter}}}" MaxWidth="{Binding RelativeSource={RelativeSource FindAncestor, 
              AncestorType={x:Type ScrollViewer}}, Path=ViewportWidth}" >
            <TextBlock VerticalAlignment="Top" TextWrapping="Wrap" FontSize="14" Text="{Binding Path=CID}" />
                 <StackPanel Orientation="Horizontal" Margin="0,5,0,0" >
                    <TextBlock>
                        <Label Foreground="{DynamicResource DisabledForegroundBrush}" >Posted by</Label>
                        <Label Foreground="{DynamicResource DisabledForegroundBrush}" VerticalContentAlignment="Top" Content="{Binding Path=ACID}" />
                    </TextBlock>
                    <TextBlock>
                         <Label Foreground="{DynamicResource DisabledForegroundBrush}" Margin="3,0,0,0">at</Label>
                        <Label Foreground="{DynamicResource DisabledForegroundBrush}" Margin="3,0,3,0" VerticalContentAlignment="Top" Content="{Binding Path=Type}" />
                    </TextBlock>
                    <TextBlock>
                        <Label Foreground="{DynamicResource DisabledForegroundBrush}">(</Label>
                        <Label Foreground="{DynamicResource DisabledForegroundBrush}" VerticalContentAlignment="Top" Content="{Binding Path=Route}" />
                        <Label Foreground="{DynamicResource DisabledForegroundBrush}">)</Label>
                    </TextBlock>
                </StackPanel>

            </StackPanel>
    </DataTemplate>

    <Style x:Key="ItemsListBox" TargetType="{x:Type ListBoxItem}">
        <Setter Property="SnapsToDevicePixels" Value="true"/>
        <Setter Property="Background" Value="{DynamicResource Transparent}"/>
        <Setter Property="BorderBrush" Value="{DynamicResource Transparent}"/>
        <Setter Property="BorderBrush" Value="{DynamicResource Transparent}"/>
        <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
    </Style>

<ListBox x:Name="ListViewFlightPlans" Grid.Column="0" ItemTemplate="{DynamicResource ItemsTemplate}" 
                         MaxWidth="800" ScrollViewer.HorizontalScrollBarVisibility="Disabled" BorderBrush="Black" BorderThickness="2,0,0,1">

            </ListBox>

戴夫

1 个答案:

答案 0 :(得分:1)

不幸的是,更改BorderBrush的{​​{1}}将无法达到预期的效果,因为带有选择突出显示的ListBoxItem位于Border的内部ControlTemplate }}

相反,您可以使用SystemColors.HighlightBrushKey键添加新的ListBoxItem资源,这是Brush用于设置选择突出显示颜色的关键。

非活动选择画笔使用SystemColors.ControlBrushKey的键,因此如果您使用透明ListBoxItem覆盖这两个键,则保证不会有任何选择颜色。您可以在this article中了解有关它的更多信息。

以下是除Brush之外的所有内容的示例:

DataTemplate
相关问题