相关疑难解决方法(0)

数据绑定动态数据

我有一组"动态数据",我需要绑定到GridControl.到目前为止,我一直在使用标准的DataTable类,它是System.Data命名空间的一部分.这工作得很好,但我被告知我不能使用它,因为它对于客户端和服务器之间的网络序列化太重了.

所以我认为我可以通过简单地使用List<Dictionary<string, object>>List表示行集合的类型来轻松复制DataTable类的"简化"版本,并且每个Dictionary表示一行,其中列名称和值为KeyValuePair类型.我可以设置Grid以使列DataField属性与Dictionary中的键匹配(就像我为DataTable的列名所做的那样).

但是做完之后

gridControl.DataSource = table;
gridControl.RefreshDataSource();
Run Code Online (Sandbox Code Playgroud)

网格没有数据......

我想我需要实施IEnumerator- 对此的任何帮助都将不胜感激!

示例调用代码如下所示:

var table = new List<Dictionary<string,object>>();

var row = new Dictionary<string, object>
{
    {"Field1", "Data1"},
    {"Field2", "Data2"},
    {"Field3", "Data3"}
};

table.Add(row);

gridControl1.DataSource = table;
gridControl1.RefreshDataSource();
Run Code Online (Sandbox Code Playgroud)

c# data-binding winforms

28
推荐指数
1
解决办法
2万
查看次数

在基类中实现IXmlSerializable时如何恢复到"默认"XML序列化?

我正在尝试序列化一个继承自实现IXmlSerializable的基类的类.

名为PropertyBag的基类是一个允许动态属性的类(Marc Gravell的学分).

我实现了IXmlSerializable,以便将动态属性(存储在Dictionary中)写为普通的xml元素.

例如,当序列化具有公共属性(非动态)名称和动态属性Age的类时,我希望它生成以下XML:

<Person>
  <Name>Tim</Name>
  <DynamicProperties>
    <Country>
      <string>USA</string>
    </Country>
  </DynamicProperties>
<Person>
Run Code Online (Sandbox Code Playgroud)

我可以让部分在基础PropertyBag类中使用WriteXml的以下实现:

public void WriteXml(System.Xml.XmlWriter writer)
    {
        writer.WriteStartElement("DynamicProperties");

        // serialize every dynamic property and add it to the parent writer
        foreach (KeyValuePair<string, object> kvp in properties)
        {
            writer.WriteStartElement(kvp.Key);

            StringBuilder itemXml = new StringBuilder();
            using (XmlWriter itemWriter = XmlWriter.Create(itemXml))
            {
                // serialize the item
                XmlSerializer xmlSer = new XmlSerializer(kvp.Value.GetType());
                xmlSer.Serialize(itemWriter, kvp.Value);                    

                // read in the serialized xml
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(itemXml.ToString());

                // write to modified …
Run Code Online (Sandbox Code Playgroud)

c# ixmlserializable

7
推荐指数
1
解决办法
2329
查看次数

通过ICustomTypeDescriptor生成WPF DataGrid AutoColumn

在测试项目中,我在以下场景中设法自动生成WPF DataGrid列,其中数据存储在Dictionary中,并通过PropertyDescriptors执行绑定:

public class People:List<Person>{
     ...
}
public class Person:Dictionary<string,string>,INotifyPropertyChanged,ICustomTypeDescriptor
{

}
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是在我的实际项目中我使用的是MVVM,因此它是People ViewModel,它继承了ViewModelBase,因此无法继承List <Person>.我尝试使用内部List <Person>实现IList <Person>,并显式将DataContext设置为IList <Person>引用,但这不起作用.

我已经看到了绑定一个双赢窗体DataGridView类似的帖子在这里,所以我不知道,如果同样的逻辑也适用于WPF和为主,到底是什么导致了ICustomTypeDescriptor实施继承名单<T>这是当被拾起当你简单地实现IList <T>时会丢失.

c# wpf icustomtypedescriptor

7
推荐指数
1
解决办法
2324
查看次数