0

我是样式的新手,需要帮助来为 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>

戴夫

4

1 回答 1

1

不幸的是,更改BorderBrushListBoxItem不会产生您想要的效果,因为Border带有选择突出显示的 是 的 内部ControlTemplateListBoxItem

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

不活动的选择画笔使用SystemColors.ControlBrushKey的键,因此如果您用透明覆盖这两者,则Brush可以保证没有任何选择颜色。您可以在本文中阅读更多相关信息。

这是一个除您之外的所有内容的示例DataTemplate

<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid.Resources>
        <x:Array x:Key="data" Type="{x:Type sys:String}">
            <sys:String>a </sys:String>
            <sys:String>bb</sys:String>
            <sys:String>ccc</sys:String>
            <sys:String>dddd</sys:String>
        </x:Array>
    </Grid.Resources>
    <ListBox ItemsSource="{StaticResource data}">
        <ListBox.Resources>
            <Style TargetType="{x:Type ListBoxItem}">
                <Style.Resources>
                    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
                    <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent"/>
                </Style.Resources>
                <Style.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="Foreground" Value="Black"/>
                    </Trigger>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Foreground" Value="Gold"/>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </ListBox.Resources>
    </ListBox>
</Grid>
于 2009-03-17T17:57:27.197 回答