ListBoxItem选择颜色已禁用,但内容也是如此

时间:2013-04-25 21:09:30

标签: wpf wpf-4.0

关于上一个问题,我有以下代码禁用蓝色背景:

<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>

我注意到的是,现在所有内容中的组合框都具有相同的透明选择风格。我需要做什么才能使选择只对ListBoxItem透明而不是对其内容透明?

2 个答案:

答案 0 :(得分:1)

您可以将这些画笔设置为DataTemplate中的原始值,如下所示:

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

                    </Expander>
                </Border>
            </DataTemplate>

答案 1 :(得分:0)

您必须定位ListBoxItem并添加到ListBox Resources

<ListBox>
  <ListBox.Resources>
    <Style TargetType="{x:Type ListBoxItem}">
        <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.Resources>
</ListBox>