WPF ListView不起作用

时间:2016-09-20 21:46:24

标签: c# wpf listview binding

我遇到了WPF ListView(使用元素绑定)的问题,因为我尝试通过注入ItemsSource磁盘列表来初始化它,并且没有从元素获得视觉反馈。我使用提供的here示例编写了我的代码。

以下是相关的代码部分:

设置ListView

    private void viewDisk_Click(object sender, RoutedEventArgs e)
    {
        List<DiskDetails> data = new List<DiskDetails>();
        foreach(Disk disk in disks)            
            data.Add(new DiskDetails(disk.GetVolumeHeader().DiskName, disk.GetVolumeHeader().DiskOwner,disk.GetVolumeHeader().ProdDate));               
        disksList.ItemsSource = data;
    }

DiskDetails类

public class DiskDetails
    {
        public string diskName { get; set; }
        public string diskOwner { get; set; }
        public string cDate { get; set; }
        public DiskDetails(string dN, string dO,string cD)
        {
            diskName = dN;
            diskOwner = dO;
            cDate = cD;
        }
    }

WPF ListView

    <Grid Grid.Column="0">
        <ListView x:Name="disksList" VerticalAlignment="Top" Height="250" SelectionChanged="disksList_SelectionChanged">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Disk Name" Width="108" DisplayMemberBinding="{Binding Name}" />
                    <GridViewColumn Header="Disk Owner" Width="108" DisplayMemberBinding="{Binding Age}" />
                    <GridViewColumn Header="Creation Date" Width="108" DisplayMemberBinding="{Binding Mail}" />
                </GridView>
            </ListView.View>
        </ListView>
        <Button x:Name="viewDisk" Content="View Disk" Width="90" Height="40" VerticalAlignment="Bottom" Margin="0,0,0,15" Click="viewDisk_Click"/>
    </Grid>

感谢。

1 个答案:

答案 0 :(得分:2)

看起来您的DisplayMemberBinding没有从示例代码中更改。尝试更改绑定以匹配DiskDetails的属性。例如。 DisplayMemberBinding =&#34; {Binding Name}&#34;应更改为DisplayMemberBinding =&#34; {Binding diskName}&#34;