Windows 8.1,XAML 绑定列表视图但不显示第一项 [英] Windows 8.1, XAML binding listview but not showing the first item

查看:32
本文介绍了Windows 8.1,XAML 绑定列表视图但不显示第一项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的代码

    <ListView ScrollViewer.VerticalScrollMode="Disabled" ScrollViewer.VerticalScrollBarVisibility="Hidden" HorizontalAlignment="Stretch" VerticalContentAlignment="Stretch" VerticalAlignment="Stretch" SelectionMode="None" Grid.Row="3" Grid.ColumnSpan="3" CanReorderItems="False" CanDragItems="False" IsRightTapEnabled="False" IsItemClickEnabled="True" BorderBrush="White" Background="White" ItemClick="lvGroupInfoCVS_ItemClick"  RequestedTheme="Light" FontFamily="Helvetica" x:Name="lvGroupInfoCVS" ItemsSource="{Binding Source={StaticResource MyCollection}}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <Grid Width="{Binding ElementName=lvGroupInfoCVS, Path=ActualWidth}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="4*"/>
                            <ColumnDefinition Width="1*"/>
                            <ColumnDefinition  Width="1*"/>
                        </Grid.ColumnDefinitions>
                    <TextBlock Grid.Row="0" FontFamily="Helvetica" Grid.Column="0" Text="{Binding Name}" Foreground="DarkGray" FontSize="23" HorizontalAlignment="Left"/>
                    <TextBlock Grid.Row="0"  FontFamily="Helvetica" Grid.Column="1" Text="{Binding EventDate}" Foreground="DarkGray" FontSize="23" HorizontalAlignment="Center"/>
                    <TextBlock Grid.Column="2" FontFamily="Helvetica" Grid.Row="0"  Text="{Binding StartTime}" Foreground="DarkGray" FontSize="23" HorizontalAlignment="Center"/>
                    </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

我的所有数据都正确显示,但我的第一个项目是空白的,尽管它是可点击的.我尝试更改宽度和高度,但仍然存在相同的问题

All my data shows up properly but my first item is blank, although it is clickable. I tried changing the width and height but I still have the same issue

环境:Windows 8.1、XAML、C#

Environment: Windows 8.1, XAML, C#

我尝试查看其他类似的 stackoverflow 问题,但没有运气

I tried looking into the other similar stackoverflow questions but had no luck

推荐答案

我遇到了同样的问题,我刚刚修复了它!
我们俩都试图通过绑定到 ListView.ActualWidth 来设置 Grid Width,但它在第一个元素被渲染之前不起作用,所以第一个元素总是有 0 宽度 :(

I had the same problem and I just fixed it!
Both of us tried to set Grid Width by binding to ListView.ActualWidth, but it's not working until first element will be rendered, so first elemnt always have 0 Width :(

要解决此问题,请执行此操作(基于 此答案 by @Kievic

To solve this, please do this (based on this answer by @Kievic

设置 ListView.ItemContainerStyle 与Horizo​​ntalContentAlignment setter 成功.

Setting the ListView.ItemContainerStyle with a HorizontalContentAlignment setter makes the trick.

<ListView.ItemContainerStyle>
    <Style TargetType="ListViewItem">
        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
    </Style>
</ListView.ItemContainerStyle>

这篇关于Windows 8.1,XAML 绑定列表视图但不显示第一项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆