WPF更改ListboxItem背景选择时的颜色

时间:2012-06-16 23:03:15

标签: wpf pixelsense

我有一个表格列表框如下

<s:SurfaceListBox x:Name="viewList" Height="200" Width="Auto" SelectedIndex="0"
                ItemsSource="{Binding Source={StaticResource views}, XPath=Views/View}"
                DisplayMemberPath="@Title"                    
                SelectionChanged="viewList_SelectionChanged" Grid.Row="2" VerticalAlignment="Bottom" HorizontalContentAlignment="Stretch" ScrollViewer.VerticalScrollBarVisibility="Disabled">
        <s:SurfaceListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch"/>
            </ItemsPanelTemplate>
        </s:SurfaceListBox.ItemsPanel>
    </s:SurfaceListBox>

我已经将App.xaml文件中的项目设置为如下

<Style TargetType="{x:Type s:SurfaceListBoxItem}">

        <Style.Resources>
            <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" 
                     Color="#0071bc"/>
        </Style.Resources>
        <Setter Property="Height" Value="200"></Setter>
        <Setter Property="Width" Value="480"></Setter>
        <Setter Property="BorderThickness" Value="0,0,0,0"></Setter>
        <Setter Property="Padding" Value="20,20,0,20"></Setter>
        <Setter Property="Margin" Value="0"></Setter>
        <Setter Property="Foreground" Value="White"></Setter>
        <Setter Property="BorderBrush" Value="White"></Setter>
        <Setter Property="FontSize" Value="57"></Setter>
        <Setter Property="FontFamily" Value="TitilliumText22L"></Setter>
        <Setter Property="FontWeight" Value="Bold"></Setter>
        <Setter Property="TextBlock.TextAlignment" Value="Left"></Setter>
        <Setter Property="TextBlock.HorizontalAlignment" Value="Stretch"></Setter>
        <Setter Property="Background">
            <Setter.Value>
                <LinearGradientBrush>
                    <GradientStop Color="#3c3c3c" Offset="0"></GradientStop>
                    <GradientStop Color="#383838" Offset="0.6"></GradientStop>
                    <GradientStop Color="#6d6e6e" Offset="1"></GradientStop>
                </LinearGradientBrush>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Foreground" Value="White"></Setter>
                <Setter Property="BorderBrush" Value="#0071bc"></Setter>
                <Setter Property="Background" Value="#0071bc"></Setter>
            </Trigger>
        </Style.Triggers>
    </Style>

我的问题是,所选项目的背景颜色保持白色,而不是样式中描述的颜色。任何指针都将不胜感激

2 个答案:

答案 0 :(得分:2)

不确定您是否找到了答案,但我遇到了同样的问题。尝试在您的样式中设置表面颜色资源。您可以在此处找到表面颜色列表:http://msdn.microsoft.com/en-us/library/microsoft.surface.presentation.surfacecolors_properties.aspx

确保导入SurfaceColors的命名空间

xmlns:sc="clr-namespace:Microsoft.Surface.Presentation;assembly=Microsoft.Surface.Presentation"

然后更新SurfaceListBox样式中的表面颜色:

<s:SurfaceListBox>
     <s:SurfaceListBox.ItemContainerStyle>
         <Style TargetType="s:SurfaceListBoxItem">
              <Style.Resources>
                 <SolidColorBrush x:Key="{x:Static sc:SurfaceColors.ListBoxItemSelectionBackgroundBrushKey }" Color="#FFA2CD65"/>
              </Style.Resources>
          </Style>
     </s:SurfaceListBox.ItemContainerStyle>
</s:SurfaceListBox>

答案 1 :(得分:0)

看起来您正在尝试创建自己的ListBox。通常没有必要这样做,但如果你认为必须这样做,请确保覆盖GetContainerForItemOverride方法

    protected override DependencyObject GetContainerForItemOverride()
    {
        return new SurfaceListBoxItem();
    }