C# 将字符串值从列表传递到datagrid
我正在尝试编写一个方法,该方法通过Load按钮从文件系统中的某个位置获取xml.config文件,对其进行解析,然后处理元素属性type和mapTo中的特定字符串,从这些属性中获取一些子字符串。问题是,我得到的不是表中的字符串,而是一些行。有人知道问题出在哪里吗 按钮和方法调用如下所示:C# 将字符串值从列表传递到datagrid,c#,xml,wpf,datagrid,C#,Xml,Wpf,Datagrid,我正在尝试编写一个方法,该方法通过Load按钮从文件系统中的某个位置获取xml.config文件,对其进行解析,然后处理元素属性type和mapTo中的特定字符串,从这些属性中获取一些子字符串。问题是,我得到的不是表中的字符串,而是一些行。有人知道问题出在哪里吗 按钮和方法调用如下所示: private void button1_Click(object sender, RoutedEventArgs e) { OpenFileDialog fDialog
private void button1_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog fDialog = new OpenFileDialog();
fDialog.Title = "Open XML file";
fDialog.Filter = "XML files|*.config";
fDialog.InitialDirectory = @"C:\";
bool? control = fDialog.ShowDialog();
if (control.Value)
{
var filePath = fDialog.FileName;
ReadAdvancedConfigFile(filePath);
}
}
private void ReadAdvancedConfigFile(string path)
{
XElement root = null;
root = XElement.Load(new XmlTextReader(path));
if (root != null)
{
XNamespace ns = "http://schemas.microsoft.com/practices/2010/unity";
var registers = root.Element(ns + "unity").Element(ns + "container").Descendants(ns + "register");
if (registers.Count() > 0)
{
var tipList = registers.Select(x => x.Attribute("type").Value);
var mapToList = registers.Select(x => x.Attribute("mapTo").Value);
List<string> listresult = new List<string>();
List<string> listresultm = new List<string>();
foreach (string tpl in tipList)
{
int end = tpl.IndexOf(',');
int start = tpl.LastIndexOf('.', (end == -1 ? tpl.Length - 1 : end)) + 1;
string result = tpl.Substring(start, (end == -1 ? tpl.Length : end) - start);
listresult.Add(result);
}
foreach (string mpl in mapToList)
{
int endm = mpl.IndexOf(',');
int startm = mpl.LastIndexOf('.', (endm == -1 ? mpl.Length - 1 : endm)) + 1;
string resultm = mpl.Substring(startm, (endm == -1 ? mpl.Length : endm) - startm);
listresultm.Add(resultm);
}
int maxLenList = Math.Max(listresult.Count, listresultm.Count);
for (int i = 0; i < maxLenList; i++)
{
if (i < listresult.Count && i < listresultm.Count)
{
_obsCollection.Add(new Tuple<string, string>(listresult[i], listresultm[i]));
}
else if (i >= listresult.Count)
{
_obsCollection.Add(new Tuple<string, string>(string.Empty, listresultm[i]));
}
else if (i >= listresultm.Count)
{
_obsCollection.Add(new Tuple<string, string>(listresultm[i], string.Empty));
}
}
tabela.ItemsSource = _obsCollection;
}
}
}
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
</configSections>
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<container name="container">
<register name="configService" type="Web.Common.Interfaces.IConfigService, Web.Common"
mapTo="Web.Common.Services.ConfigServiceImpl, Web.Common">
<lifetime type="singleton" />
<constructor>
<param name="res" value="Resources.ClientStrings"> </param>
<param name="configFile" value="webclient.config"> </param>
</constructor>
<!--<property name="LocalisationService" dependencyName="LocalisationService" />-->
<!--This is a property injection from the language plugin -->
</register>
<register name="scaleCoefConfigService" type="Web.WebClient.Services.IScaleCoefConfigService, Web.WebClient.TDMSWebApp"
mapTo="Web.WebClient.Services.Implementations.ScaleCoefConfigServiceImpl, Web.WebClient.TDMSWebApp">
<lifetime type="singleton" />
<constructor>
<param name="configService">
<dependency name="configService"/>
</param>
</constructor>
</register>
<register name="sessionService" type="Web.Common.Interfaces.ISessionService, Web.Common"
mapTo="Web.Common.Services.SessionServiceImpl, Web.Common">
<lifetime type="singleton" />
</register>
<register name="licenseManagerService" type="Web.Common.Interfaces.ILicenseManagementService, Web.Common"
mapTo="Web.Common.Services.LicenseManagementServiceImpl, Web.Common">
<lifetime type="singleton" />
</register>
</container>
</unity>
</configuration>
方法,该方法从xml文件获取值,为每个属性类型和mapTo获取子字符串,将它们放入两个单独的列表中,并尝试将列表内容写入datagrid,如下所示:
private void button1_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog fDialog = new OpenFileDialog();
fDialog.Title = "Open XML file";
fDialog.Filter = "XML files|*.config";
fDialog.InitialDirectory = @"C:\";
bool? control = fDialog.ShowDialog();
if (control.Value)
{
var filePath = fDialog.FileName;
ReadAdvancedConfigFile(filePath);
}
}
private void ReadAdvancedConfigFile(string path)
{
XElement root = null;
root = XElement.Load(new XmlTextReader(path));
if (root != null)
{
XNamespace ns = "http://schemas.microsoft.com/practices/2010/unity";
var registers = root.Element(ns + "unity").Element(ns + "container").Descendants(ns + "register");
if (registers.Count() > 0)
{
var tipList = registers.Select(x => x.Attribute("type").Value);
var mapToList = registers.Select(x => x.Attribute("mapTo").Value);
List<string> listresult = new List<string>();
List<string> listresultm = new List<string>();
foreach (string tpl in tipList)
{
int end = tpl.IndexOf(',');
int start = tpl.LastIndexOf('.', (end == -1 ? tpl.Length - 1 : end)) + 1;
string result = tpl.Substring(start, (end == -1 ? tpl.Length : end) - start);
listresult.Add(result);
}
foreach (string mpl in mapToList)
{
int endm = mpl.IndexOf(',');
int startm = mpl.LastIndexOf('.', (endm == -1 ? mpl.Length - 1 : endm)) + 1;
string resultm = mpl.Substring(startm, (endm == -1 ? mpl.Length : endm) - startm);
listresultm.Add(resultm);
}
int maxLenList = Math.Max(listresult.Count, listresultm.Count);
for (int i = 0; i < maxLenList; i++)
{
if (i < listresult.Count && i < listresultm.Count)
{
_obsCollection.Add(new Tuple<string, string>(listresult[i], listresultm[i]));
}
else if (i >= listresult.Count)
{
_obsCollection.Add(new Tuple<string, string>(string.Empty, listresultm[i]));
}
else if (i >= listresultm.Count)
{
_obsCollection.Add(new Tuple<string, string>(listresultm[i], string.Empty));
}
}
tabela.ItemsSource = _obsCollection;
}
}
}
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
</configSections>
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<container name="container">
<register name="configService" type="Web.Common.Interfaces.IConfigService, Web.Common"
mapTo="Web.Common.Services.ConfigServiceImpl, Web.Common">
<lifetime type="singleton" />
<constructor>
<param name="res" value="Resources.ClientStrings"> </param>
<param name="configFile" value="webclient.config"> </param>
</constructor>
<!--<property name="LocalisationService" dependencyName="LocalisationService" />-->
<!--This is a property injection from the language plugin -->
</register>
<register name="scaleCoefConfigService" type="Web.WebClient.Services.IScaleCoefConfigService, Web.WebClient.TDMSWebApp"
mapTo="Web.WebClient.Services.Implementations.ScaleCoefConfigServiceImpl, Web.WebClient.TDMSWebApp">
<lifetime type="singleton" />
<constructor>
<param name="configService">
<dependency name="configService"/>
</param>
</constructor>
</register>
<register name="sessionService" type="Web.Common.Interfaces.ISessionService, Web.Common"
mapTo="Web.Common.Services.SessionServiceImpl, Web.Common">
<lifetime type="singleton" />
</register>
<register name="licenseManagerService" type="Web.Common.Interfaces.ILicenseManagementService, Web.Common"
mapTo="Web.Common.Services.LicenseManagementServiceImpl, Web.Common">
<lifetime type="singleton" />
</register>
</container>
</unity>
</configuration>
private void ReadAdvancedConfigFile(字符串路径)
{
XElement root=null;
root=XElement.Load(新的XmlTextReader(路径));
if(root!=null)
{
XNS=”http://schemas.microsoft.com/practices/2010/unity";
var registers=root.Element(ns+“unity”).Element(ns+“container”).subjects(ns+“register”);
if(registers.Count()>0)
{
var tipList=寄存器。选择(x=>x.Attribute(“type”).Value);
var mapToList=寄存器。选择(x=>x.Attribute(“mapTo”).Value);
List listresult=新列表();
List ListResultTM=新列表();
foreach(tipList中的字符串tpl)
{
int end=tpl.IndexOf(',');
int start=tpl.LastIndexOf('.',(end=-1?tpl.Length-1:end))+1;
字符串结果=tpl.Substring(开始,(结束==-1?tpl.Length:end)-start);
listresult.Add(结果);
}
foreach(映射列表中的字符串mpl)
{
int endm=mpl.IndexOf(',');
int startm=mpl.LastIndexOf('.',(endm==-1?mpl.Length-1:endm))+1;
字符串resultm=mpl.Substring(startm,(endm==-1?mpl.Length:endm)-startm);
添加(resultm);
}
int maxLenList=Math.Max(listresult.Count、ListResultTM.Count);
对于(int i=0;i=listresult.Count)
{
_添加(新元组(string.Empty,listrestm[i]);
}
如果(i>=ListResultTM.Count)为else,则为
{
_Add(新元组(listrestm[i],string.Empty));
}
}
tabela.ItemsSource=\u obsCollection;
}
}
}
XML.config文件的内容如下所示:
private void button1_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog fDialog = new OpenFileDialog();
fDialog.Title = "Open XML file";
fDialog.Filter = "XML files|*.config";
fDialog.InitialDirectory = @"C:\";
bool? control = fDialog.ShowDialog();
if (control.Value)
{
var filePath = fDialog.FileName;
ReadAdvancedConfigFile(filePath);
}
}
private void ReadAdvancedConfigFile(string path)
{
XElement root = null;
root = XElement.Load(new XmlTextReader(path));
if (root != null)
{
XNamespace ns = "http://schemas.microsoft.com/practices/2010/unity";
var registers = root.Element(ns + "unity").Element(ns + "container").Descendants(ns + "register");
if (registers.Count() > 0)
{
var tipList = registers.Select(x => x.Attribute("type").Value);
var mapToList = registers.Select(x => x.Attribute("mapTo").Value);
List<string> listresult = new List<string>();
List<string> listresultm = new List<string>();
foreach (string tpl in tipList)
{
int end = tpl.IndexOf(',');
int start = tpl.LastIndexOf('.', (end == -1 ? tpl.Length - 1 : end)) + 1;
string result = tpl.Substring(start, (end == -1 ? tpl.Length : end) - start);
listresult.Add(result);
}
foreach (string mpl in mapToList)
{
int endm = mpl.IndexOf(',');
int startm = mpl.LastIndexOf('.', (endm == -1 ? mpl.Length - 1 : endm)) + 1;
string resultm = mpl.Substring(startm, (endm == -1 ? mpl.Length : endm) - startm);
listresultm.Add(resultm);
}
int maxLenList = Math.Max(listresult.Count, listresultm.Count);
for (int i = 0; i < maxLenList; i++)
{
if (i < listresult.Count && i < listresultm.Count)
{
_obsCollection.Add(new Tuple<string, string>(listresult[i], listresultm[i]));
}
else if (i >= listresult.Count)
{
_obsCollection.Add(new Tuple<string, string>(string.Empty, listresultm[i]));
}
else if (i >= listresultm.Count)
{
_obsCollection.Add(new Tuple<string, string>(listresultm[i], string.Empty));
}
}
tabela.ItemsSource = _obsCollection;
}
}
}
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
</configSections>
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<container name="container">
<register name="configService" type="Web.Common.Interfaces.IConfigService, Web.Common"
mapTo="Web.Common.Services.ConfigServiceImpl, Web.Common">
<lifetime type="singleton" />
<constructor>
<param name="res" value="Resources.ClientStrings"> </param>
<param name="configFile" value="webclient.config"> </param>
</constructor>
<!--<property name="LocalisationService" dependencyName="LocalisationService" />-->
<!--This is a property injection from the language plugin -->
</register>
<register name="scaleCoefConfigService" type="Web.WebClient.Services.IScaleCoefConfigService, Web.WebClient.TDMSWebApp"
mapTo="Web.WebClient.Services.Implementations.ScaleCoefConfigServiceImpl, Web.WebClient.TDMSWebApp">
<lifetime type="singleton" />
<constructor>
<param name="configService">
<dependency name="configService"/>
</param>
</constructor>
</register>
<register name="sessionService" type="Web.Common.Interfaces.ISessionService, Web.Common"
mapTo="Web.Common.Services.SessionServiceImpl, Web.Common">
<lifetime type="singleton" />
</register>
<register name="licenseManagerService" type="Web.Common.Interfaces.ILicenseManagementService, Web.Common"
mapTo="Web.Common.Services.LicenseManagementServiceImpl, Web.Common">
<lifetime type="singleton" />
</register>
</container>
</unity>
</configuration>
这就是我为字符串对创建元组的方法。在这个类中还有ReadAdvancedConfigFile方法和Load按钮的定义,当然:
public partial class CreateAreaDialogWindow : System.Windows.Window
{
ObservableCollection<Tuple<string, string>> _obsCollection = new ObservableCollection<Tuple<string, string>>();
public CreateAreaDialogWindow()
{
InitializeComponent();
}
}
公共部分类CreateAreaDialogWindow:System.Windows.Window
{
ObservableCollection_obsCollection=新的ObservableCollection();
公共CreateAreaDialogWindow()
{
初始化组件();
}
}
datagrid的XAML代码如下:
<DataGrid AutoGenerateColumns="False" Height="146" HorizontalAlignment="Left" Margin="34,275,0,0" Name="tabela" VerticalAlignment="Top" Width="384" SelectionChanged="tabela_SelectionChanged" />
希望我没有把事情弄得太复杂 您设置了
AutoGenerateColumns=“False”
,但未提供任何列
试试这个,它会起作用:
<DataGrid AutoGenerateColumns="True" />
或者查看如何创建列。您可以添加问题的屏幕截图吗。“我还不太清楚。”帕特里克霍夫曼用线条截图的datagrid?或者别的什么?是的,这就是我的意思。@PatrickHofman因为某种原因不会发送照片。。。我会继续尝试。但它基本上是一个窗口,只有一个加载按钮,下面是DataGrid。在DataGrid中,我在两列中有行而不是字符串。我认为这一行可能有问题:tabela.ItemsSource=\u obscolection;因为我不确定ItemsSource能不能像那样工作?我知道了。我自己也看到了这个问题,但用一张图片更容易看出。别再担心了。看我的答案。我爱你,伙计!:D成功了!:)谢谢。我知道这很愚蠢,但我仍在学习,这可能很难:)谢谢你的链接。我需要它:)因为接下来我必须研究如何为每个值在该选项卡中添加复选框:)