【发布时间】:2014-04-15 12:30:46
【问题描述】:
我正在开发基于网络的应用程序,该应用程序使用 C# 与网络设备连接,前端位于 WPF 上。问题是我想在运行特定命令后提取数据,并且在提取后我希望它显示在 DataGrid 上。数据正在根据需要使用正则表达式正确提取,但我想在 Datagrid 上显示的部分没有显示,但它在控制台上正确显示。代码是:
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>
简而言之,我不明白如何在数据网格上显示输出,因为它在控制台上显示。请帮忙??
【问题讨论】:
-
dg.ItemsSource = ipmac;怎么样?