繁体   English   中英

有没有办法将属性/变量动态绑定到 C# WPF DataGrid 中的对象

[英]Is there a way to dynamically bind properties /variables to objects in C# WPF DataGrid

我正在为自己编写一个小工具,帮助我为飞行模拟器绑定操纵杆。 由于每个轴必须在游戏中单独绑定,我想用我的工具在高层次上解决这个问题。 长话短说,我需要读取带有数据的文本文件,这些数据告诉工具当前存在多少架飞机,以及我是否想让操纵杆也绑定到那架飞机。

因此,为了让用户知道他的个人资料是否也适用于此,他需要点击复选框。 所以在运行时创建标题和列是没有问题的,可以用这个来完成。

void InitDGSelected()
   {
     DataGridTextColumn IdTxtColumn = new DataGridTextColumn();
     IdTxtColumn.Header = "ID";
     IdTxtColumn.Binding = new Binding("ID");
     DGAdded.Columns.Add(IdTxtColumn); //DGAdded is the DataGrid
     for(int i=1; i<MainStructure.Planes.Length; ++i)
     {
         DataGridCheckBoxColumn pcbc = new DataGridCheckBoxColumn();
         pcbc.Header = MainStructure.Planes[i];
         pcbc.Binding = new Binding(MainStructure.Planes[i]);
         pcbc.IsReadOnly = false;
         DGAdded.Columns.Add(pcbc);
     }    
   }

然而,用数据填充它是我的问题开始的地方。 WPF 需要在运行时始终具有固定长度的属性对象,因为它们被编译,例如我正在使用的这个:

public class SearchQueryResults
   {
       public string ID { get; set; }
       public string AIRCRAFT { get; set; }
       public string DESCRIPTION { get; set; }
   }

但由于我不想编译每个游戏补丁或再次更改我的工具,我想知道是否有一种方法可以动态地拥有带有动态绑定的 object。 我已经试过了

Dictionary<object, object> plswork = new Dictionary<object, object>();
    plswork.Add("ID", ri[i].ID);
    for (int j = 1; j < MainStructure.Planes.Length; ++j)
    {
        if (ri[i].GetStateAircraft(MainStructure.Planes[i]) == PlaneState.ACTIVE)
            plswork.Add(MainStructure.Planes[i], true);
        else if(!plswork.ContainsKey(MainStructure.Planes[i]))
            plswork.Add(MainStructure.Planes[i], false);
    }
    DGAdded.Items.Add(plswork);

与 <string, object> 或一维数组相同,其中一个字符串后跟应该具有相同位长的布尔值,但是我卡住了。 请有人帮忙!

干杯!

因此解决方案是创建一个 DataTable 并将其 defaultView 用作数据网格的 ItemSource。

这是一个小片段,我是如何解决的。 祝大家编码愉快。 请查看其他链接的主题。

    DataTable GetEmptyDTForSelection()
    {
        System.Data.DataTable dt = new DataTable("Data");
        dt.Columns.Add("ID", typeof(string));
        for (int i = 1; i < MainStructure.Planes.Length; ++i)
        {
            dt.Columns.Add(MainStructure.Planes[i]+"_cb", typeof(bool));
            dt.Columns.Add(MainStructure.Planes[i]+"_desc", typeof(string));
        }

        return dt;
    }

    void RefreshDGSelected()
    {
        DGAdded.Items.Clear();
        List<RelationItem> ri = Current.AllRelations();
        System.Data.DataTable dt = GetEmptyDTForSelection();

        for (int i=0; i<ri.Count; ++i)
        {
            object[] row = new object[1 + (MainStructure.Planes.Length - 1)*2 ];
            row[0] = ri[i].ID;
            int k = 1;
            for (int j = 1; j < MainStructure.Planes.Length; ++j)
            {
                if (ri[i].GetStateAircraft(MainStructure.Planes[j]) == PlaneState.ACTIVE)
                {
                    row[k] = true;
                }
                else
                {
                    row[k] = false;
                }
                ++k;
                row[k] = ri[i].GetInputDescription(MainStructure.Planes[j]);
                ++k;                    
            }
            dt.Rows.Add(row);
        }
        DGAdded.ItemsSource = dt.DefaultView;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM