0

我有一个surfacelistbox如下

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

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

4

2 回答 2

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>
于 2012-06-27T15:11:12.550 回答
0

看起来您正在尝试创建自己的 ListBox。通常不需要这样做,但如果您觉得必须这样做,请确保覆盖 GetContainerForItemOverride 方法

    protected override DependencyObject GetContainerForItemOverride()
    {
        return new SurfaceListBoxItem();
    }
于 2012-06-17T05:26:05.753 回答