SurfaceListBox选择的项目模板

时间:2011-02-24 21:47:21

标签: c# wpf pixelsense

我正在使用Microsoft Surface SDK,但我无法设置所选项目的样式。

到目前为止,我有:

<s:SurfaceListBox Name="listy" Background="Transparent"
                    FontSize="50" Foreground="White" BorderBrush="White"
                    HorizontalContentAlignment="Center"
                    Margin="5,5,5,100" SelectionMode="Single">
    <s:SurfaceListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding}" Foreground="White" />
        </DataTemplate>
    </s:SurfaceListBox.ItemTemplate>
    <s:SurfaceListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListBoxItem}">
                        <ContentPresenter HorizontalAlignment="Center" />
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsSelected" Value="True">
                                <Setter Property="Background" Value="Grey" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </s:SurfaceListBox.ItemContainerStyle>
</s:SurfaceListBox>

但是失败了,互联网上根本没有这方面的教程 - 我搜索了几个小时。

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

我认为问题在于没有使用该背景。您可以将ContentPresenter包裹在Border中并将其定位在触发器

<ControlTemplate TargetType="{x:Type s:SurfaceListBoxItem}">
    <Border x:Name="Border">
        <ContentPresenter HorizontalAlignment="Center" />
    </Border>
    <ControlTemplate.Triggers>
        <Trigger Property="IsSelected" Value="True">
            <Setter TargetName="Border" Property="Background" Value="Gray"/>
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>