0

我有一个<ListBox>自定义<ListBox.ItemTemplate><DataTemplate>在其中:

 <ListBox>
      <ListBox.ItemTemplate>
           <DataTemplate>
               <Border BorderBrush="Black" BorderThickness="2" CornerRadius="5">
                    <Image Source="{Binding Picture}" />
                </Border>
           </DataTemplate>
      </ListBox.ItemTemplate>
</ListBox>

现在,当我选择ListBoxItem蓝色行选择时,它会变得很难看。我想改变它。我只想为边框的背景着色,而不是别的。我也想改变MouseOver行为。我已经尝试过触发器,但ContentPresenter没有 Background 属性。

升级版:

好吧,我已经设法改变了MouseEnterand的背景MouseLeave

    <EventTrigger RoutedEvent="Border.MouseEnter">
         <BeginStoryboard>
              <Storyboard >
                <ColorAnimation Storyboard.TargetProperty="Background.Color"
                    To="LightBlue" Duration="0:0:0.03"/>
              </Storyboard>
         </BeginStoryboard>
   </EventTrigger>

但仍然无法更改Background选择项目的时间。我正在尝试:

  <Trigger  Property="ListBoxItem.IsSelected" Value="True">
      <Setter Property="Background" Value="Red" />
  </Trigger>

不工作

4

2 回答 2

2

您要查找的颜色位于 ListBoxItem 模板内的两个触发器中,而不是 ItemTemplate。要更改这一点,您需要编辑 ListBox 的 ItemContainerStyle。这是可以用作起点的默认值:

    <ListBox>
        <ListBox.ItemContainerStyle>
            <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="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                                    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
                                </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.ItemContainerStyle>
    </ListBox>
于 2010-04-06T03:00:49.587 回答
0

您可以通过使用触发器来做到这一点。我在我的一个项目中有这样的事情:

<Trigger Property="IsSelected" Value="true">
  <Setter Property="Panel.ZIndex" Value="1"/>
  <Setter Property="Background" TargetName="Bd" Value="{StaticResource TabItemSelectedBackground}"/>
</Trigger>

虽然它不会更改边框颜色,但它显示了如何更改背景。因此,也许尝试将其设置为空。此触发器是自定义样式的一部分,其中使用 IsMouseOver 属性实现跟踪。

高温高压

于 2010-04-05T19:00:43.913 回答