WPF ListView doesn't work

本文关键字:work ListView doesn WPF | 更新日期: 2023-09-27 18:07:54

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

以下是相关的代码部分:

设置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;
    }
<<p> DiskDetails类/strong>
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>

谢谢。

WPF ListView doesn't work

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