【问题标题】:DataGrid is not updating rows from List(of ) objectDataGrid 没有从 List(of ) 对象更新行
【发布时间】:2012-03-10 18:10:39
【问题描述】:

我已经在另一个项目中使用了这些东西,但现在不行了。 我不知道为什么。

  1. 将 DataGrid ItemsSource 设置为 {Binding}
  2. 创建新列表(属于我的班级)
  3. 创建公开列表Dim MyList As New List(of myclass)
  4. 创建实例时绑定DataGrid.DataContext = MyList
  5. 将 DataGrid 列定义设置为 myclass 属性上的某些项

我在名单上添加了我的第一堂课 MyList.Add(New myclass()) 现在,这个项目是可见的。但是在创建实例和数据网格可见之后,行不会更新。 MyList.Add(New myclass()) 并且没有第二行可见。

只有在做的时候 MyDataGrid1.Items.Refresh 项目已正确更新。 但是据我了解, List(of ) 类已经包含了INotifyPropertyChanged 接口。

任何想法为什么会失败?

这里有一些额外的代码

        <DataGrid.Columns>
            <DataGridTextColumn         Header="N°"                 Binding="{Binding Path=LineNumber, StringFormat='{}{0:000}'}"           Width="SizeToCells"     MinWidth="30" IsReadOnly="True"  />

            <DataGridTextColumn         Header="HEX"                Binding="{Binding HexText}"         Width="SizeToHeader"    MinWidth="80" IsReadOnly="True"  FontWeight="Bold"                    />
            <DataGridTextColumn         Header="TextNormalized"     Binding="{Binding Description}"     Width="*"     MinWidth="80" IsReadOnly="True"  FontSize="12"       />
        </DataGrid.Columns>
    </DataGrid>

一些代码隐藏:

 Private ListData As New List(Of clsHexRowData)

实例:

Public Sub New()

    ' This call is required by the designer.
    InitializeComponent()

    Me.dgv.ItemsSource = Me.ListData
    Me.ListData.Clear()
    Dim item As New clsQueueItem
    item.FromString("Hallo")
    Me.AddItem(item)
End Sub

这是可见的,但是在初始化页面后添加新行时,项目不会更新,需要dgv.Items.Refresh,我不明白为什么。

【问题讨论】:

    标签: .net wpf data-binding binding properties


    【解决方案1】:

    使用 ObservableCollection(of myclass) 而不是列表。您将在添加或删除项目时看到更改。

    编辑

     //add
     Application.Current.Dispatcher.BeginInvoke((Action)(()=> { Me.ListData.Add(new Item());}))
    

    【讨论】:

    • 答案很快。多谢。现在我明白了,为什么我的另一个(我的第一个)项目成功了。因为我没有那样使用更新的行。谢谢盲人
    • 亲爱的盲人。 VS 现在抛出一个错误,因为我需要在 Dispatcher 线程中添加和删除项目。有没有办法避免进入UI线程,调度线程?
    • 您必须将它们添加到 ui 线程中。最简单的方法是将您的添加和删除委托给 ui 线程。
    • 这是一个很好的线程,它现在是如何工作的,也许在下一个 .net 10rem.net/blog/2012/01/16/…
    猜你喜欢
    • 2013-11-07
    • 2023-03-11
    • 2012-03-15
    • 2015-10-30
    • 1970-01-01
    • 2021-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多