- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我在将对象列表绑定(bind)到数据 GridView 或控件时遇到了问题。其实我想要什么,我有课说Person
, Address
和 Contact
. Person
类有 3 个属性,一个是字符串类型的名称,Add
地址类型,最后一个是 Cont
类型 Contact
.通过谷歌搜索,我发现我必须创建 CustomTypeDescriptor
我创建的类,它只适用于一个类 Contact
或 Address
.当我们放两次时,它会显示不能重复的编译时错误 [TypeDescriptionProvider(typeof(MyTypeDescriptionProvider<Contact>))]
.
我该如何解决这个问题。
这里我提供了我正在尝试实现的示例代码,
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.AutoGenerateColumns = false;
dataGridView1.Columns.Add("Name", "Name");
dataGridView1.Columns.Add("City", "City");
dataGridView1.Columns.Add("ContactName", "ContactName");
dataGridView1.Columns["Name"].DataPropertyName = "Name";
dataGridView1.Columns["City"].DataPropertyName = "Add_City";
dataGridView1.Columns["ContactName"].DataPropertyName = "Cont_ContactName";
List<Person> PersonList = PersonProxy.GetPersonCollection();
dataGridView1.DataSource = PersonList;
}
}
public class PersonProxy
{
public static List<Person> GetPersonCollection()
{
List<Person> persList = new List<Person>();
persList.Add(new Person
{
Name = "Awadhendra",
Add = new Address
{
City = "Allahabad"
},
Cont = new Contact
{
ContactName = "Awadh"
}
});
persList.Add(new Person
{
Name = "Alok",
Add = new Address
{
City = "Allahabad"
},
Cont = new Contact
{
ContactName = "Alok"
}
});
persList.Add(new Person
{
Name = "Ankit",
Add = new Address
{
City = "Lucknow"
},
Cont = new Contact
{
ContactName = "Ankit"
}
});
persList.Add(new Person
{
Name = "Swati",
Add = new Address
{
City = "Lucknow"
},
Cont = new Contact
{
ContactName = "Awadh"
}
});
return persList;
}
}
[TypeDescriptionProvider(typeof(MyTypeDescriptionProvider<Contact>))]
public class Person
{
public string Name { get; set; }
public Address Add { get; set; } ////How to get address and contact both for binding.
public Contact Cont { get; set; } ////Write now am getting Contact
}
public class Address
{
public string City { get; set; }
}
public class Contact
{
public string ContactName { get; set; }
}
public class MyTypeDescriptionProvider<T> : TypeDescriptionProvider
{
private ICustomTypeDescriptor td;
public MyTypeDescriptionProvider()
: this(TypeDescriptor.GetProvider(typeof(Person)))
{
}
public MyTypeDescriptionProvider(TypeDescriptionProvider parent)
: base(parent)
{
}
public override ICustomTypeDescriptor GetTypeDescriptor(Type objectType, object instance)
{
if (td == null)
{
td = base.GetTypeDescriptor(objectType, instance);
td = new MyCustomTypeDescriptor(td, typeof(T));
}
return td;
}
}
public class SubPropertyDescriptor : PropertyDescriptor
{
private PropertyDescriptor _subPD;
private PropertyDescriptor _parentPD;
public SubPropertyDescriptor(PropertyDescriptor parentPD, PropertyDescriptor subPD, string pdname)
: base(pdname, null)
{
_subPD = subPD;
_parentPD = parentPD;
}
public override bool IsReadOnly { get { return false; } }
public override void ResetValue(object component) { }
public override bool CanResetValue(object component) { return false; }
public override bool ShouldSerializeValue(object component)
{
return true;
}
public override Type ComponentType
{
get { return _parentPD.ComponentType; }
}
public override Type PropertyType { get { return _subPD.PropertyType; } }
public override object GetValue(object component)
{
return _subPD.GetValue(_parentPD.GetValue(component));
}
public override void SetValue(object component, object value)
{
_subPD.SetValue(_parentPD.GetValue(component), value);
OnValueChanged(component, EventArgs.Empty);
}
}
public class MyCustomTypeDescriptor : CustomTypeDescriptor
{
Type typeProperty;
public MyCustomTypeDescriptor(ICustomTypeDescriptor parent, Type type)
: base(parent)
{
typeProperty = type;
}
public override PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
PropertyDescriptorCollection cols = base.GetProperties(attributes);
string propertyName = "";
foreach (PropertyDescriptor col in cols)
{
if (col.PropertyType.Name == typeProperty.Name)
propertyName = col.Name;
}
PropertyDescriptor pd = cols[propertyName];
PropertyDescriptorCollection children = pd.GetChildProperties();
PropertyDescriptor[] array = new PropertyDescriptor[cols.Count + children.Count];
int count = cols.Count;
cols.CopyTo(array, 0);
foreach (PropertyDescriptor cpd in children)
{
array[count] = new SubPropertyDescriptor(pd, cpd, pd.Name + "_" + cpd.Name);
count++;
}
PropertyDescriptorCollection newcols = new PropertyDescriptorCollection(array);
return newcols;
}
}
谢谢,
最佳答案
基于 MSDN
There are two ways to associate a
TypeDescriptionProvider
with aTypeDescriptor
:
- At design time, when the target class can be assigned the appropriate
TypeDescriptionProviderAttribute
tag.- At run time, when one of the
AddProvider
methods of theTypeDescriptor
class can be called. These overloaded methods require either the target object or its class type.
所以只需在运行时添加它们:
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.AutoGenerateColumns = false;
dataGridView1.Columns.Add("Name", "Name");
dataGridView1.Columns.Add("City", "City");
dataGridView1.Columns.Add("ContactName", "ContactName");
dataGridView1.Columns["Name"].DataPropertyName = "Name";
dataGridView1.Columns["City"].DataPropertyName = "Add_City";
dataGridView1.Columns["ContactName"].DataPropertyName = "Cont_ContactName";
List<Person> PersonList = PersonProxy.GetPersonCollection();
//add them here
System.ComponentModel.TypeDescriptor.AddProvider((new MyTypeDescriptionProvider<Address>()), typeof(Person));
System.ComponentModel.TypeDescriptor.AddProvider((new MyTypeDescriptionProvider<Contact>()), typeof(Person));
dataGridView1.DataSource = PersonList;
}
关于c# - c# winforms 应用程序中的 datagridview/controls 中的子级对象属性绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14259080/
我有一个 winform 应用程序和一个可观察的设置,如下所示: Form form = new Form(); Label lb = new Label(); form.Controls.Add(l
在 Windows 窗体中实现多项选择选项的最佳方法是什么?我想从列表中强制执行单个选择,从默认值开始。 看起来 ComboBox 是一个不错的选择,但是有没有办法指定一个非空白的默认值? 我可以在代
如何在 WinForm 应用程序中保护我的 ConnectionString? 最佳答案 你不能。尽管您可以在 app.config 文件中加密连接字符串,但应用程序需要能够解密它,因此,始终可以检索
有谁知道像 DotNetBar 那样的 Winforms 面包屑样式导航。 http://www.devcomponents.com/dotnetbar/BreadCrumbHorizontalTre
我正在寻找在 Windows 窗体 C# 中实现多选启用列表框的方法。 有什么建议? 谢谢。 最佳答案 只需添加一个 ListBox控制和设置属性:SelectionMode = SelectionM
我有一个简单的 WinForms 应用程序,用于输入测试用例。自从我将此应用程序升级到 .NET 4.0 并将新的标签页添加到标签页控件以根据 XSD 架构验证 XML 以来,该应用程序一直随机崩溃。
在老式的 MFC 中,DDX 例程内置了表单条目的验证。例如,可以自动检查用户是否在用于数字输入的文本框中输入了字符串。 Winforms中有这样的机制吗?显然,可以为“onChange”等类型的事件
我主要具有 ASP.Net 背景,并具有一些 MVC 知识。我也做了一些 Silverlight 和 MVVM,但是我现在即将转向 Winforms,我对它的经验很少,所以我想知道如何处理 MVP。
简单的问题,虽然办公室里似乎没有人知道,而且我找不到一个好的方法来问谷歌这个问题。在 winforms 中,如果您有一个处理事件的函数(在本例中是在 focusLost 上),那么该函数是否与触发该事
在 Winform 中,我有一个组框,其中有几个文本框控件。如果我删除组框,文本框也会被删除。它们以某种方式与 Groupbox 联系在一起,尽管我没有故意做任何事情来实现这种情况。问题 - 如何删除
我可以在哈希表中设置表单元素: $Hash = @{} $Hash.Main = New-Object System.Windows.Forms.Form $Hash.Main.Left = 0 $H
我是 Windows 开发新手。我开发了一个 WinForm 应用程序,它与串行设备通信并在图表上绘制数据。该应用程序应每天 7/7 24 小时运行。代码执行正确,但执行几个小时后,UI 卡住,操作系
有没有办法记录 Win Forms 应用程序中的所有点击?我想拦截点击并记录该操作以及导致该操作的控件的名称。 这可能吗? 提前致谢。 更新:我正在寻找一个应用程序范围的解决方案,是否没有办法将监听器
我不知道这是否会影响其他控件,但对于列表框和选中列表框,列表框的底部仅以一定的间隔随表单调整大小。 假设我有一个表单和一个列表框,该列表框与表单边缘的所有边都有 2px 间隙,并锚定在所有四个边上。现
我在大多数 WinForms 控件的设计 View 中看到“Tag”属性。我从未使用过此标签,并且想知道为什么我要使用它。 最佳答案 它允许您使用控件存储一些自己的数据。它在树控件中最有用,您可能希望
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 7年前关闭。 Improve thi
如果有,有人吗?鱼眼 (菜单/ Pane )窗体在 Ajax 应用程序中常见的控件: 例如:http://interface.eyecon.ro/demos/fisheye.html 用 google
这是 Determine Label Size based upon amount of text and font size in Winforms/C# 的倒数. 给定一个高度固定但宽度可变的矩形
我们公司正在研究为我们的开发团队采购 22"显示器。当前唯一的问题是我们的用户将使用较小的屏幕。 我们尝试使用屏幕网格工具(gridmove 和 nvidia 的 utils),但它们并不完全现实。
我有一个 winforms 应用程序,它在网络服务请求期间被锁定 我已经尝试使用 doEvents 来保持应用程序解锁,但它仍然不够响应, 我怎样才能绕过这个锁定,让应用程序始终响应? 最佳答案 最好
我是一名优秀的程序员,十分优秀!