[英]Adding Rows and Columns to a WPF DataGrid at Run-Time
所有人,我是WPF的新手。 我想讀取一個.resx文件以及所有具有不同區域性的相關文件; 例如,默認的“ en-GB” .resx文件名為“ SomeFile.resx”和“ SomeFile.de-DE.resx”(德語類似物)。 當然,我可以擁有與不同文化相關的大量不同的.resx文件。
因此,我想將所有不同的文化資源讀入單個DataGrid
,以便獲得類似
Resource Index | Resource Key | English (Default) | German
1 | SomeKey1 | Run Script | Skript Ausführen
2 | SomeKey2 | OK | OK
等。 我的問題是將這些數據添加到名為dataGrid
的DataGrid
的最佳方法。 所以我目前將可用的非默認.resx文件放入Dictionary<string, string>
Dictionary<string, string> cultureDict = new Dictionary<string, string>();
然后,我計划使用ResXResourceReader
遍歷不同的.resx文件(針對多種文化),像這樣讀取.resx文件
Dictionary<string, string> resourceMap = new Dictionary<string, string>();
public static void Func(string fileName)
{
ResXResourceReader rsxr = new ResXResourceReader(fileName);
foreach (DictionaryEntry d in rsxr)
{
resourceMap.Add(d.Key.ToString(),d.Value.ToString());
}
rsxr.Close();
}
public string GetResource(string resourceId)
{
return resourceMap[resourceId];
}
我計划創建一個List<Dictionary<string, string>>
來容納來自讀者的不同文化的信息,然后遍歷List
並添加行。
我的問題是,考慮到區域性的數量是可變的並且可能很大,將這些閱讀器返回的數據放入DataGrid
的最佳和最有效的方法是什么?
謝謝你的時間。
注意:我可以添加列
DataGridTextColumn itemColumn = new DataGridTextColumn();
itemColumn.Header = "Item Number";
itemColumn.Binding = new Binding("Item Number");
dataGrid.Columns.Add(itemColumn);
但這是為這種特殊情況添加行的最佳方法。
public static void Display_Grid(DataGrid d, List<string> S1)
{
ds = new DataSet();
DataTable dt = new DataTable();
ds.Tables.Add(dt);
DataColumn cl = new DataColumn("Item Number", typeof(string));
cl.MaxLength = 200;
dt.Columns.Add(cl);
int i = 0;
foreach (string s in S1)
{
DataRow rw = dt.NewRow();
rw["Item Number"] = S1[i];
i++;
}
d.ItemsSource = ds.Tables[0].AsDataView();
}
使用observablecollection ItemCollection在datagrid中添加新行
itemmodel model=new itemmodel ();
model.name='Rahul';
ItemCollection.add(model);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.