Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF数据网格绑定错误?_C#_Wpf_Datagrid - Fatal编程技术网

C# WPF数据网格绑定错误?

C# WPF数据网格绑定错误?,c#,wpf,datagrid,C#,Wpf,Datagrid,我正在开发基于网络的应用程序。该应用程序使用C#与网络设备连接。前端在WPF上。问题是,我想在运行特定命令后提取数据,提取后,我希望数据显示在DataGrid上。根据需要使用正则表达式正确提取数据,但我想显示的部分Datagrid未显示,但在控制台上正确显示。代码为: public class IPMAC { public string ip { get; set; } public string mac { get; set; } } List<IPMAC> ip

我正在开发基于网络的应用程序。该应用程序使用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 });
公共类IPMAC
{
公共字符串ip{get;set;}
公共字符串mac{get;set;}
}
List ipmac=新列表();
字符串模式=@“(F8-F7-D3-00\S+);
MatchCollection matches=Regex.matches(stringData,pattern);
foreach(匹配中的匹配)
{
WriteLine(“硬件地址:{0}”,match.Groups[1].Value);
Add(新的ipmac(){mac=match.Groups[1].Value});
}
字符串模式2=@“(192.168.1\S+);
MatchCollection matchesIP=Regex.Matches(stringData,pattern2);
foreach(matchesIP中的Match)
{
WriteLine(“IP地址:{0}”,match.Groups[1].Value);
Add(新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上显示控制台上显示的输出。请帮助???

datagrid
中显示
IPMAC
列表的最简单方法是,在填充列表后,通过设置
ItemsSource
从代码中:

dg.ItemsSource = ipmac;
或者,您可以通过以下步骤使用
数据绑定

  • 正确设置
    DataContext
    。因为数据绑定从当前数据上下文解析绑定路径
  • ipmac
    声明为
    observedcollection
    类型的公共属性
    observedcollection
    具有内置机制,可在向集合中添加或从集合中删除项时通知用户界面刷新。数据绑定无法与成员/字段一起使用
  • 项目资源
    绑定到
    ipmac
    属性
演示上述步骤的代码段:

//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" ... />
//将ipmac声明为公共属性
公共可观测集合ipmac{get;set;}
//在构造函数中:初始化ipmac并设置DataContext
ipmac=新的可观测集合();
this.DataContext=this;
//在XAML中:将ItemsSource绑定到ipmac

关于
dg.ItemsSource=ipmac;
?它解决了我的问题,现在感觉很好,但还有一件事,它在一行上显示mac地址,在第二行显示ip,这会是什么问题?这很奇怪。没有更多信息,不知道。你确定这不是用错误数据填充的集合吗?