更改ListView项目选择样式

时间:2014-04-27 09:51:33

标签: windows-store-apps

如何更改Windows应用商店应用中ListView中的ListView项目选择样式?我想更改颜色,边距并删除复选框。我试图在Blend中更改各种模板,但我无法弄清楚这一个: - (。

enter image description here

XAML的代码:

<Page
x:Class="WindowsStoreListViewSelectionTest.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:WindowsStoreListViewSelectionTest"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Page.DataContext>
    <local:BasicData/>
</Page.DataContext>

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <ListView ItemsSource="{Binding ListData}" SelectedIndex="1">
    </ListView>
</Grid>

C#-code:

public class BasicData
{
    public BasicData()
    {
        _ListData = new ObservableCollection<object>();
        ListData.Add("Alfa");
        ListData.Add("Beta");
        ListData.Add("Gamma");
    }

    private ObservableCollection<object> _ListData;

    public ObservableCollection<object> ListData
    {
        get
        {
            return _ListData;
        }
    }
}

2 个答案:

答案 0 :(得分:11)

所以问题终于解决了:-)。以下是其他初学者使用Windows 8.1项目的分步指南。它适用于Blend(一个很棒的工具 - 花一些时间来学习如何使用它),但我很确定它对Visual Studio来说大致相同。

右键单击列表视图并选择:

  • 修改其他模板
  • 编辑生成的项目容器(ItemContainerStyle)
  • 编辑副本
  • 输入名称

然后生成一个如下所示的样式:

<Style x:Key="ListViewItemStyle1" TargetType="ListViewItem">
    <Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}"/>
    <Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}"/>
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="TabNavigation" Value="Local"/>
    <Setter Property="IsHoldingEnabled" Value="True"/>
    <Setter Property="Margin" Value="0,0,18,2"/>
    <Setter Property="HorizontalContentAlignment" Value="Left"/>
    <Setter Property="VerticalContentAlignment" Value="Top"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ListViewItem">
                <ListViewItemPresenter 
                    CheckHintBrush="{ThemeResource ListViewItemCheckHintThemeBrush}" 
                    CheckBrush="{ThemeResource ListViewItemCheckThemeBrush}" 
                    ContentMargin="4" 
                    ContentTransitions="{TemplateBinding ContentTransitions}" 
                    CheckSelectingBrush="{ThemeResource ListViewItemCheckSelectingThemeBrush}" 
                    DragForeground="{ThemeResource ListViewItemDragForegroundThemeBrush}" 
                    DragOpacity="{ThemeResource ListViewItemDragThemeOpacity}" 
                    DragBackground="{ThemeResource ListViewItemDragBackgroundThemeBrush}" 
                    DisabledOpacity="{ThemeResource ListViewItemDisabledThemeOpacity}" 
                    FocusBorderBrush="{ThemeResource ListViewItemFocusBorderThemeBrush}" 
                    HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" 
                    Padding="{TemplateBinding Padding}" 
                    PointerOverBackgroundMargin="1" 
                    PlaceholderBackground="{ThemeResource ListViewItemPlaceholderBackgroundThemeBrush}" 
                    PointerOverBackground="{ThemeResource ListViewItemPointerOverBackgroundThemeBrush}" 
                    ReorderHintOffset="{ThemeResource ListViewItemReorderHintThemeOffset}" 
                    SelectedPointerOverBorderBrush="{ThemeResource ListViewItemSelectedPointerOverBorderThemeBrush}" 
                    SelectionCheckMarkVisualEnabled="True" 
                    SelectedForeground="{ThemeResource ListViewItemSelectedForegroundThemeBrush}" 
                    SelectedPointerOverBackground="{ThemeResource ListViewItemSelectedPointerOverBackgroundThemeBrush}" 
                    SelectedBorderThickness="{ThemeResource ListViewItemCompactSelectedBorderThemeThickness}" 
                    SelectedBackground="{ThemeResource ListViewItemSelectedBackgroundThemeBrush}" 
                    VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

通过更改SelectionCheckMarkVisualEnabled值可以轻松删除复选标记。

答案 1 :(得分:0)

您正在寻找的是ListViewItem Style。这包含边框,选择字形以及您想要更改的其他所有内容的定义。它通常通过ListView属性在ItemContainerStyle中设置。