非常感谢任何帮助。对不起,如果我的代码是初级代码。我是C#的新手。
问题
我使用 MahApps 在DataGrids
中动态创建多个flyout
。 DataGrids
由 CSV 文件(ConvertCSVtoDataTable()
)填充。我希望用户能够对DataGrids
进行更改,完成后,DataGrids
值将替换 CSV 文件(UpdateDataGridParameter()
)。
UI树:Flyout
> StackPanel
> GroupBox
> DataGrid
问题
DG.SelectAllCells()
未从用户更改DataGrid
中进行选择。如何获取DataGid
的VISUAL UI表示或将DT属性绑定到数据更改事件。我希望我能正确解释这一点。如果您有任何可以帮助我的问题,请发布,我会尽快回复。感谢
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
namespace SpiderRoll.Classes
{
public class DataGridProperties
{
private int IDValue;
public int ID
{
get { return IDValue; }
set { IDValue = value; }
}
private string HeaderValue;
public string Header
{
get { return HeaderValue; }
set { HeaderValue = value; }
}
private string DescriptionValue;
public string Description
{
get { return DescriptionValue; }
set { DescriptionValue = value; }
}
private string NameValue;
public string Name
{
get { return NameValue; }
set { NameValue = value; }
}
private string FilePathValue;
public string FilePath
{
get { return FilePathValue; }
set { FilePathValue = value; }
}
private DataTable DTValue;
public DataTable DT
{
get
{
return DTValue;
}
set
{
DTValue = value;
}
}
public static DataGridProperties DataGridObject(List<DataGridProperties> TBP, int ID = 0, string Name = "")
{
foreach (var tb in TBP)
{
if (tb.ID == ID || tb.Name == Name)
{
return tb;
}
}
return null;
}
public static int FindDataGridID(List<DataGridProperties> DGP, string Name = "")
{
int i = 0;
foreach (var dg in DGP)
{
if (dg.Name == Name)
{
return i;
}
i++;
}
return -1;
}
public static GroupBox DataGridPropertieStackPanel(DataGridProperties DataGrid) // DataGridProperties DataGrid
{
GroupBox GB = new GroupBox();
GB.Header = DataGrid.Header;
StackPanel SPMain = new StackPanel();
SPMain.Orientation = System.Windows.Controls.Orientation.Vertical;
System.Windows.Controls.Label LBDescription = new System.Windows.Controls.Label();
LBDescription.Content = DataGrid.Description;
LBDescription.Margin = new Thickness(10, 0, 0, 0);
SPMain.Children.Add(LBDescription);
StackPanel SP = new StackPanel();
SP.Name = DataGrid.Name;
SP.Orientation = System.Windows.Controls.Orientation.Horizontal;
SP.Margin = new Thickness(10);
System.Windows.Controls.DataGrid DG = new System.Windows.Controls.DataGrid();
DG.Name = DataGrid.Name;
DG.CanUserAddRows = false;
DG.ItemsSource = DataGrid.DT.DefaultView;
SP.Children.Add(DG);
SPMain.Children.Add(SP);
GB.Content = SPMain;
return GB;
}
public static DataTable ConvertCSVtoDataTable(string FilePath)
{
StreamReader sr = new StreamReader(FilePath);
string[] headers = sr.ReadLine().Split(',');
string[] firstLine = sr.ReadLine().Split(',');
DataTable dt = new DataTable();
DataColumn column = new DataColumn();
DataRow fl = dt.NewRow();
int idx = 0;
foreach (string header in headers)
{
//If bool is in first row, turn the column into a checkbox.
if (firstLine[idx].ToLower() == "true" || firstLine[idx].ToLower() == "false")
{
column = dt.Columns.Add(header, typeof(bool));
}
else
{
column = dt.Columns.Add(header, typeof(string));
column.ReadOnly = true;
}
if (header.EndsWith("~"))
{
column.ReadOnly = true;
}
//Reading and building the first row
fl[idx] = firstLine[idx];
idx++;
}
//Adding first row
dt.Rows.Add(fl);
while (!sr.EndOfStream)
{
string line = sr.ReadLine();
string[] rows = line.Split(',');
DataRow dr = dt.NewRow();
for (int i = 0; i < headers.Length; i++)
{
dr[i] = rows[i];
}
dt.Rows.Add(dr);
}
return dt;
}
public static void UpdateDataGridParameter(DataGrid DG)
{
StringBuilder sb = new StringBuilder();
IEnumerable<string> columnNames = DG.Columns.Cast<DataGridColumn>().
Select(column => column.Header.ToString());
sb.AppendLine(string.Join(",", columnNames));
DG.UnselectAllCells();
DG.SelectAllCells();
foreach (DataRowView row in DG.SelectedItems)
{
IEnumerable<string> fields = row.Row.ItemArray.Select(field => field.ToString());
sb.AppendLine(string.Join(",", fields));
}
DG.UnselectAllCells();
var filePath = @"C:\IO\" + DG.Name + ".csv";
File.WriteAllText(filePath, sb.ToString());
}
}
}
在Class Bundle中,我创建了一个DataGrids列表
private List<DataGridProperties> dataGridList;
public List<DataGridProperties> DataGridList
{
get
{
return dataGridList;
}
set
{
dataGridList = value;
}
}
在Main
中,我迭代DataGridlist
并通过调用StackPanels
函数填充DataGridPropertieStackPanel
。
foreach (var item in bundle.DataGridList)
{
if (item.Name == DataGrid.Name)
{
sPanelParameters.Children.Add(DataGridProperties.DataGridPropertieStackPanel(item));
}
}
答案 0 :(得分:0)
更改值后,DataGrid会更新绑定的DataView。您不必选择和取消选择网格行,只需直接访问数据视图即可。稍微更改了代码版本:
The sum = 5.