【问题标题】:C# Binding to Gridview column displaying class nameC# 绑定到显示类名的 Gridview 列
【发布时间】:2015-09-24 10:51:27
【问题描述】:

我正在尝试将数据绑定到列表视图中的 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”的类“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;

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

编辑:我已经将它与使用数据模板的列表框一起使用,也许列表视图也需要一个?

【问题讨论】:

  • 显示 pkschemes 定义。
  • pkschemes = JsonConvert.DeserializeObject>(sj);但这并不重要,因为该列表工作正常,它在测试时输出所有正确的数据。它只是不起作用的绑定
  • 您不需要 ItemsSource="{Binding }" 因为您直接在代码隐藏中设置它,如后面的代码所示。
  • 没有区别
  • 它显示类“pkscheme”的名称?

标签: c# .net wpf listview binding


【解决方案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; }
    }
}

【讨论】:

    【解决方案2】:

    尝试删除 ItemsSource="{Binding}"

    【讨论】:

    • 什么都没做,我已经多次在拥有和没有拥有它之间徘徊:/
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-01
    • 1970-01-01
    相关资源
    最近更新 更多