C#绑定到显示类名的Gridview列

时间:2015-07-07 05:20:33

标签: c# .net wpf listview binding

我正在尝试将数据绑定到listview中的gridview列,但它只输出类的名称(namespace.pkscheme)而不是实际数据。

XAML:

<ListView ItemsSource="{Binding}" Margin="10,20,10,10" Name="pkcsTable">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Name" Width="195" DisplayMemberBinding="{Binding name}" />
            <GridViewColumn Header="Author" Width="195" DisplayMemberBinding="{Binding author}" />
            <GridViewColumn Header="Screenshot" Width="195" DisplayMemberBinding="{Binding screenshot}" />
            <GridViewColumn Header="Download" Width="195" DisplayMemberBinding="{Binding download}" />
        </GridView>
    </ListView.View>
</ListView>

C#:我有一个名为'pkschemes'的班级'pkscheme'的列表

public class pkscheme
{
public string name { get; set; }
public string author { get; set; }
public string screenshot { get; set; }
public string download { get; set; }
}

pkcs_w = new UserControl1();
pkcs_w.Width = 800;
pkcs_w.Height = 500;
pkcs_w.pkcsTable.ItemsSource = pkschemes;

我做错了什么?我在这里按照教程http://www.wpf-tutorial.com/listview-control/listview-with-gridview/

编辑:我已经使用了一个使用数据模板的列表框,也许listview还需要一个?

2 个答案:

答案 0 :(得分:1)

我已经尝试过您的代码了。它对我有用。这就是我尝试的代码的样子:

<强> UserControl1.xaml:

<UserControl x:Class="minimizeApp.UserControl1"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<Grid>
    <ListView ItemsSource="{Binding}" Margin="10,20,10,10" Name="pkcsTable">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Name" Width="195" DisplayMemberBinding="{Binding name}" />
                <GridViewColumn Header="Author" Width="195" DisplayMemberBinding="{Binding author}" />
                <GridViewColumn Header="Screenshot" Width="195" DisplayMemberBinding="{Binding screenshot}" />
                <GridViewColumn Header="Download" Width="195" DisplayMemberBinding="{Binding download}" />
            </GridView>
        </ListView.View>
    </ListView>
</Grid>

<强> MainWindow.xaml:

<Window x:Class="minimizeApp.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid x:Name="grid1">

</Grid>

<强> MainWindow.xaml.cs:

 namespace minimizeApp
    {

    public partial class MainWindow : Window
    {
        List<pkscheme> pkschemes = new List<pkscheme>();

        UserControl1 pkcs_w = null;
        public MainWindow()
        {
            InitializeComponent();

            //1st row
            pkscheme pk = new pkscheme();
            pk.name = "pk";
            pk.author = "pkAuthor";
            pk.screenshot = "pkScreenshot";
            pk.download = "pkDownload";

            //2nd row
            pkscheme pk1 = new pkscheme();
            pk1.name = "pk1";
            pk1.author = "pkAuthor1";
            pk1.screenshot = "pkScreenshot1";
            pk1.download = "pkDownload1";

            pkschemes.Add(pk);
            pkschemes.Add(pk1);

            pkcs_w = new UserControl1();
            pkcs_w.Width = 800;
            pkcs_w.Height = 500;
            pkcs_w.pkcsTable.ItemsSource = pkschemes;

            grid1.Children.Add(pkcs_w);
        }
    }

    public class pkscheme
    {
        public string name { get; set; }
        public string author { get; set; }
        public string screenshot { get; set; }
        public string download { get; set; }
    }
}

答案 1 :(得分:0)

尝试删除ItemsSource =&#34; {Binding}&#34;