您好我正在使用wpf应用程序,有一个数据网格,我在提取一段字符串后显示我的结果。问题是数据显示在网格中的两个但我希望它在单行.Code是:
C#
public ObservableCollection<IPMAC> ipmac { get; set; }
public MainWindow()
{
InitializeComponent();
ipmac = new ObservableCollection<IPMAC>();
this.DataContext = this;
}
string pattern = @"(F8-F7-D3-00\S+)";
MatchCollection matches = Regex.Matches(stringData, pattern);
string pattern2 = @"(192.168.1\S+)";
MatchCollection matchesIP = Regex.Matches(stringData, pattern2);
foreach (Match match in matches)
{
Console.WriteLine("Hardware Address : {0}", match.Groups[1].Value);
ipmac.Add(new IPMAC() { mac = match.Groups[1].Value });
}
foreach (Match match in matchesIP)
{
Console.WriteLine("IP Address : {0}", match.Groups[1].Value);
ipmac.Add(new IPMAC() { ip = match.Groups[1].Value });
}
}
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>][1]
任何人都可以告诉我如何在一行中显示结果。任何帮助都会非常明显吗?
答案 0 :(得分:2)
将您的解析更改为以下内容:
var item = new IPMAC();
foreach (Match match in matches)
{
Console.WriteLine("Hardware Address : {0}", match.Groups[1].Value);
item.mac = match.Groups[1].Value;
}
foreach (Match match in matchesIP)
{
Console.WriteLine("IP Address : {0}", match.Groups[1].Value);
item.ip = match.Groups[1].Value;
}
ipmac.Add(item);
答案 1 :(得分:1)
//Create your Obj Outside
IPMAC ipObj = new IPMAC();
foreach (Match match in matches)
{
Console.WriteLine("Hardware Address : {0}", match.Groups[1].Value);
ipObj.mac = match.Groups[1].Value;
}
foreach (Match match in matchesIP)
{
Console.WriteLine("IP Address : {0}", match.Groups[1].Value);
ipObj.ip = match.Groups[1].Value;
}
ipmac.Add(ipObj);
private void DataGrid_Loaded(object sender, RoutedEventArgs e)
{
dg.ItemsSource = ipmac;
}