【问题标题】:Change background color of content in ListBoxItem更改 ListBoxItem 中内容的背景颜色
【发布时间】:2013-04-25 15:02:23
【问题描述】:

我是 WPF 新手;我有一个包含扩展器的列表框(扩展器周围有边框):

<ListBox Background="Transparent" BorderBrush="Transparent">
    <ListBox.Style>
        <Style>
            <Style.Resources>
                <!-- Background of selected item when focussed -->
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
                <!-- Background of selected item when not focussed -->
                <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
            </Style.Resources>
        </Style>
    </ListBox.Style>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Border Margin="5" BorderThickness="2" BorderBrush="LightGray" CornerRadius="5">
                <Expander IsExpanded="True" Background="#f7f7f7">
                    <!-- Content -->
                </Expander>
            </Border>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ListBox>

我想要的行为是所选项目的扩展器背景颜色变暗(例如#e0e0e0)。我找到了让我隐藏 ListBoxItem 的背景颜色的示例(这在代码中显示),但没有很好的资源来更改 ListBoxItem 中内容的背景颜色。我知道我需要设置一个触发器来执行此操作,但我不知道如何设置它,它是 ListBox 还是 Expander 的触发器。

如何设置合适的触发器?

【问题讨论】:

  • 您可能想尝试一下 Expression Blend,它是执行此类操作的非常好的工具
  • 触发器需要一个条件来评估,你想要什么条件。触发器是基于数据值还是仅在选择或展开该行/项目时触发?

标签: wpf xaml wpf-4.0


【解决方案1】:
 <DataTemplate>
     <Border Margin="5" BorderThickness="2" BorderBrush="LightGray" CornerRadius="5">
       <Expander IsExpanded="True" Background="#f7f7f7" Name="expander">

       </Expander>
     </Border>
   <DataTemplate.Triggers>
     <DataTrigger Binding="{Binding IsSelected,RelativeSource={RelativeSource AncestorType=ListBoxItem}}" Value="True">
       <Setter Property="Background" Value="#e0e0e0" TargetName="expander"/>
     </DataTrigger>
   </DataTemplate.Triggers>
 </DataTemplate>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-17
    • 1970-01-01
    相关资源
    最近更新 更多