JimmyB
JimmyB

Reputation: 1

C# Binding to Gridview column displaying class name

I'm trying bind data to gridview columns within a listview, however it only outputs the name of the class (namespace.pkscheme) instead of the actual data.

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#: I have have a list of the class 'pkscheme' called 'pkschemes'

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;

What am I doing wrong? I followed the tutorial here http://www.wpf-tutorial.com/listview-control/listview-with-gridview/

EDIT: I've got it working with a listbox, which uses a data template, perhaps listview needs one aswell?

Upvotes: 0

Views: 1288

Answers (2)

user5088037
user5088037

Reputation:

I have tried your code. It is working for me. This is how the code which I tried looks:

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; }
    }
}

Upvotes: 1

rgstamayo
rgstamayo

Reputation: 163

Try removing ItemsSource="{Binding}"

Upvotes: 0

Related Questions