首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WPF数据绑定错误?

问WPF数据绑定错误?
EN

Stack Overflow用户
提问于 2014-04-15 12:30:46
回答 1查看 930关注 0票数 2

我正在开发基于网络的应用程序,应用程序与使用C#的网络设备连接,前端使用WPF,.The问题是,在运行特定命令之后,我想提取数据,在提取之后,我希望它显示在DataGrid.The数据上,使用regex正确提取数据,这是我需要的,但是,我想要显示的部分没有显示,但是它在控制台上显示是正确的,.Code是:

代码语言:javascript
复制
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是:

代码语言:javascript
复制
<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帮助上?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-15 12:55:44

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

代码语言:javascript
复制
dg.ItemsSource = ipmac;

也可以通过以下步骤使用DataBinding:

  • DataContext 正确地设置了。因为数据绑定从当前数据上下文解析绑定路径。
  • ipmac 将声明为 ObservableCollection.类型的公共属性。ObservableCollection内置了通知UI的机制,每当添加到集合中或从集合中删除项时,都会通知UI刷新。而且数据绑定不能与成员/字段一起工作。
  • ItemsSource将绑定到 ipmac property。

演示上述步骤的片段:

代码语言:javascript
复制
//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" ... />
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23083826

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档