c# - Combobox 中的 DisplayMember 无法正常工作

标签 c# .net winforms

我有这个方法可以将 Conta 实例添加到名为“comboContas”的 ComboBox 中:

public void AdicionaConta(Conta novaConta)
{
    comboContas.Items.Add(novaConta);
    comboContas.DisplayMember = "Titular";
}

请注意,我已将 DisplayMember 属性设置为“Titular”。这是我的 Conta 类:

public abstract class Conta
{
    public int Numero { get; set; }

    public double Saldo { get; set; }

    public Cliente Titular { get; set; }

    public override string ToString()
    {
        return "titular: " + this.Titular.Nome;
    }       
}

现在,“Titular”属于Cliente 类型。 这是 Cliente 类:

public class Cliente
{
    public string Nome { get; set; }
    public string Rg { get; set; }

    public Cliente(string nome)
    {
        this.Nome = nome;
    }

    public override string ToString()
    {
        return "ToString do Cliente: " + this.Nome;
    }
}

我想在“comboContas”组合框中显示的内容类似于“ToString do Cliente: Gabriel”。 但是,未调用 Cliente 类的 ToString 方法。相反,被调用的是来自 Conta 类。

enter image description here

这是非常简单的事情,我真的不知道发生了什么。如果我将 DisplayMember 更改为任何其他属性,它将起作用。如果我将“Titular”属性的类型更改为任何其他类型,则会调用此其他类型的 ToString()。它只是不适用于 Cliente

最佳答案

您的代码有问题(在每次添加之后而不是提前设置?)因为它确实按预期工作。检查一下:

using System;
using System.Windows.Forms;

namespace Tests
{
    public class Conta
    {
        public int Numero { get; set; }
        public double Saldo { get; set; }
        public Cliente Titular { get; set; }
        public override string ToString()
        {
            return "titular: " + this.Titular.Nome;
        }
    }
    public class Cliente
    {
        public string Nome { get; set; }
        public string Rg { get; set; }
        public Cliente(string nome)
        {
            this.Nome = nome;
        }
        public override string ToString()
        {
            return "ToString do Cliente: " + this.Nome;
        }
    }
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            var form = new Form { Padding = new Padding(16) };
            var comboBox = new ComboBox { Dock = DockStyle.Top, Parent = form };
            comboBox.DisplayMember = "Titular";
            comboBox.Items.AddRange( new []
            {
                new Conta { Titular = new Cliente("Victor") },
                new Conta { Titular = new Cliente("Mauricio") },
                new Conta { Titular = new Cliente("csni") },
            });
            Application.Run(form);
        }
    }
}

结果:

enter image description here

关于c# - Combobox 中的 DisplayMember 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32658132/

相关文章:

c# - 从 System.Drawing.Bitmap 加载 WPF BitmapImage

c# - 将 XML 可序列化公共(public)字段重构为自动属性是否会破坏旧实例的反序列化?

c# - WPF:如何将大量大图像快速加载到包装面板中?

c# - 这两种事务处理方式有什么区别

C#/.NET Timers 和 Win32 Sleep 函数都不准确

c# - 有什么简单的方法可以在 C# 中创建方法并动态设置其主体吗?

c# - 为什么 double 型变量返回 Nan?

c# - 无法在 .Net Core 3.1 中的 Web Api 中使用 Cors

c# - 如何让 .NET 4 应用程序的主窗体出现在最前面?

c# - 使用 DrawString 将单个字符居中