我正在开发基于网络的应用程序,应用程序与使用C#的网络设备连接,前端使用WPF,.The问题是,在运行特定命令之后,我想提取数据,在提取之后,我希望它显示在DataGrid.The数据上,使用regex正确提取数据,这是我需要的,但是,我想要显示的部分没有显示,但是它在控制台上显示是正确的,.Code是:
public class IPMAC
{
public string ip { get; set; }
public string mac { get; set; }
}
List<IPMAC> ipmac = new List<IPMAC>();
string pattern = @"(F8-F7-D3-00\S+)";
MatchCollection matches = Regex.Matches(stringData, pattern);
foreach (Match match in matches)
{
Console.WriteLine("Hardware Address : {0}", match.Groups[1].Value);
ipmac.Add(new IPMAC(){mac=match.Groups[1].Value});
}
string pattern2 = @"(192.168.1\S+)";
MatchCollection matchesIP = Regex.Matches(stringData, pattern2);
foreach (Match match in matchesIP)
{
Console.WriteLine("IP Address : {0}", match.Groups[1].Value);
ipmac.Add(new IPMAC() { ip = match.Groups[1].Value });XAML是:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="250"/>
<RowDefinition/>
</Grid.RowDefinitions>
<DataGrid Name="dg" Grid.Row="0" Height="250" AutoGenerateColumns="False" >
<DataGrid.Columns>
<DataGridTextColumn Header="Mac Addresses" Binding="{Binding Path=mac}"/>
<DataGridTextColumn Header="IP Addresses" Binding="{Binding Path=ip}"/>
</DataGrid.Columns>
</DataGrid>简单地说,我不明白如何在datagrid上显示输出,因为它显示在控制台.Please帮助上?
发布于 2014-04-15 12:55:44
用IPMAC显示DataGrid列表的最简单方法是,在列表填充后从代码中设置ItemsSource:
dg.ItemsSource = ipmac;也可以通过以下步骤使用DataBinding:
DataContext 正确地设置了。因为数据绑定从当前数据上下文解析绑定路径。ipmac 将声明为 ObservableCollection.类型的公共属性。ObservableCollection内置了通知UI的机制,每当添加到集合中或从集合中删除项时,都会通知UI刷新。而且数据绑定不能与成员/字段一起工作。ItemsSource将绑定到 ipmac property。演示上述步骤的片段:
//declare ipmac as public property
public ObservableCollection<IPMAC> ipmac { get; set; }
//In constructor : initialize ipmac and set up DataContext
ipmac = new ObservableCollection<IPMAC>();
this.DataContext = this;
//In XAML : bind ItemsSource to ipmac
<DataGrid ItemSource="{Binding ipmac}" Name="dg" ... />https://stackoverflow.com/questions/23083826
复制相似问题