1

In a Xaml/C# WinRTapplication, whenever a ListViewItemis selected, the first letter on the left gets chopped off. 我怎样才能防止这种情况?

<ListView>
    <ListViewItem Content="Hello World"/>
</ListView>

未选中和完整:

未选中和完整

选择和截断:

选择和截断

4

2 回答 2

2

You can apply a margin to an item within the ListViewItem itself like below, which will stop the cut off. Alternatively you could use a ContentTemplate as the second example.

Example 1:

<ListView>
    <ListViewItem>
        <TextBlock Text="Hello World" Margin="5,0,0,0"/>
    </ListViewItem>
</ListView>

Example 2:

<ListView>
    <ListViewItem Content="Hello World!">
        <ListViewItem.ContentTemplate>

            <DataTemplate>
                <TextBlock Text="{Binding}" Margin="5,0,0,0"/>
            </DataTemplate>

        </ListViewItem.ContentTemplate>
    </ListViewItem>
</ListView>
于 2013-06-30T16:38:03.860 回答
0

作为布兰登斯科特回答的后续行动,如果您不想应用此内联,您可以将此行为设置为样式,然后将该样式应用于单个列表项,如下所示

<Application.Resources>
    <ResourceDictionary>
        <Style x:Key="ListViewItemMagin" TargetType="ListViewItem">
            <Setter Property="ContentTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <TextBlock Text="{Binding}" Margin="5,0,0,0"/>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ResourceDictionary>
</Application.Resources>

然后,将样式应用于列表视图项

<ListView>
    <ListViewItem Content="Hello World" Style="{StaticResource ListViewItemMagin}"/>
</ListView>

如果您想默认启用此功能,您可以省略x:Key样式上的属性,它将自动应用于与TargetType;匹配的任何元素。像这样:

<Style TargetType="ListViewItem">
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate>
                <TextBlock Text="{Binding}" Margin="5,0,0,0"/>
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>
于 2013-07-01T22:17:02.830 回答