0
在上面的图片,在控制台窗口我有两个硬件地址和两个IP地址,但数据网格中只显示最后的结果会是什么造成这种情况的原因DataGrid中被跳过一个结果?
代码是:
C#:
public class IPMAC
{
public string ip { get; set; }
public string mac { get; set; }
}
public ObservableCollection<IPMAC> ipmac { get; set; }
public MainWindow()
{
InitializeComponent();
ipmac = new ObservableCollection<IPMAC>();
this.DataContext = this;
}
var item = new 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 });
item.mac = match.Groups[1].Value;
}
// ipmac.Add(item);
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 });
item.ip = match.Groups[1].Value;
}
ipmac.Add(item);
}
private void DataGrid_Loaded(object sender, RoutedEventArgs e)
{
dg.ItemsSource = ipmac;
}
XAML是:
<DataGrid
Name="dg"
Grid.Row="0"
Height="250"
ItemsSource="{Binding ipmac}"
AutoGenerateColumns="False"
IsReadOnly="True"
>
<DataGrid.Columns>
<DataGridTextColumn Header="Mac Addresses" Binding="{Binding Path=mac}" />
<DataGridTextColumn Header="IP Addresses" Binding="{Binding Path=ip}"/>
</DataGrid.Columns>
</DataGrid>
您只将一个数据添加到列表 – Sajeetharan
@Sajeetharan ..在哪里? – TheSpy
我需要在列表中添加另一个数据? – TheSpy