通过绑定更新listview(绑定初学者) [英] update listview via binding (binding beginner)
问题描述
我差点儿,但不完全:
我有一个列表视图绑定到一个observablecollection。我想在更改集合后更新UI,现在它不起作用。基本上我有一个列表视图和一个按钮。按下按钮后,新项目将添加到集合中,但UI保持不变。有人能指出我正确的方向吗?
谢谢。
XAML:
< 窗口 < span class =code-attribute>
< span class =code-attribute> xmlns = http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x = http://schemas.microsoft.com/winfx / 2006 / xaml
xmlns:c = clr-namespace:StructureItems.SList
x:Class = MainWindow
标题 = MainWindow 高度 = 350 宽度 = 525 已加载 = Window_Loaded >
< Window.Resources >
< c:ItemList x:键 = SList / >
< ; / Window.Resources >
< 网格 >
< 按钮 内容 = 按钮 HorizontalAlignment = 左 保证金 = 274,17,0,0 VerticalAlignment = Top 宽度 = 75 < span class =code-attribute> 点击 = Button_Click / >
< ListView 姓名 = LV HorizontalAlignment = 左 高度 = 300 保证金 = 10,10,0,0 VerticalAlignment = Top 宽度 = 229 ItemsSource = {StaticResource SList} >
< ListView.View >
< GridView < span class =code-keyword>>
< GridViewColumn 标题 = 代码 DisplayMemberBinding = {Binding Code} 宽度 = 50 / >
< GridViewColumn 标题 = 描述 DisplayMemberBinding = {Binding Description} / >
< / GridView >
< span class =code-keyword>< / ListView.View >
< / ListView >
< / Grid >
< / Window > ;
课程
< pre lang =vb> 命名空间 SList
公共 类 ItemList
继承 ObservableCollection( StructureItem)
公开 Sub 新()
MyBase .Add( New StructureItem( A01, Arcade))
MyBase .Add( New StructureItem( A02, Autentica))
MyBase .Add( New StructureItem( A03, Belface))
< span class =code-keyword> MyBase .Add( New StructureItem( < span class =code-string> A04, Heering))
结束 Sub
公共 重载 Sub 添加(代码 As 字符串,说明作为 字符串)
MyBase .Add( New StructureItem(代码,描述))
结束 Sub
结束 类
公开 类 StructureItem
实现 INotifyPropertyChanged
公共 事件 PropertyChanged 作为 PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Private _code As < span class =code-keyword>字符串
私有 _description 作为 < span class =code-keyword> String
公开 Sub 新( ByVal 首先作为 字符串, ByVal last 作为 字符串)
我._code = first
Me ._ description = last
End < span class =code-keyword> Sub
Public Property 代码()作为 字符串
获取
返回 我 ._ code
结束 获取
设置( ByVal 值作为 字符串)
我 ._ code = value
OnPropertyChanged( 代码)
结束 设置
结束 属性
公共 属性描述()作为 字符串
获取
< span class =code-keyword>返回 我 ._ description
结束 获取
设置( ByVal 值< span class =code-keyword>作为 字符串)
我。 _description = value
OnPropertyChanged( Description)
结束 设置
结束 属性
受保护的 Sub OnPropertyChanged(< span class =code-keyword> ByVal name As String )
RaiseEvent PropertyChanged( Me ,新 PropertyC hangedEventArgs(name))
结束 Sub
结束 类
结束 命名空间
代码落后
Imports StructureItems.SList
类 MainWindow
Dim _item 作为 新 ItemList
私有 Sub Button_Click(发件人 As 对象,e As RoutedEventArgs)
_item.Add( A05, 已更新_)
结束 Sub
结束 类
你需要在主窗口中添加一个构造函数,然后在构造函数中设置DataContext:
我 .DataContext = 我
或类似的东西。我是C#程序员,忘了VB。
I'm almost there, but not completely :
i have a listview binded to an observablecollection. I want the UI updated after i change the collection, and it does not work now. basically i have a listview and a button. After pressing the button a new item is added to the collection, but the UI stays the same. can someone points me to the right direction ?
thanks.
XAML :
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:c="clr-namespace:StructureItems.SList"
x:Class="MainWindow"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Window.Resources>
<c:ItemList x:Key="SList"/>
</Window.Resources>
<Grid>
<Button Content="Button" HorizontalAlignment="Left" Margin="274,17,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
<ListView Name="LV" HorizontalAlignment="Left" Height="300" Margin="10,10,0,0" VerticalAlignment="Top" Width="229" ItemsSource="{StaticResource SList}">
<ListView.View>
<GridView>
<GridViewColumn Header="Code" DisplayMemberBinding="{Binding Code}" Width="50"/>
<GridViewColumn Header="Description" DisplayMemberBinding="{Binding Description}"/>
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>
Classes
Namespace SList
Public Class ItemList
Inherits ObservableCollection(Of StructureItem)
Public Sub New()
MyBase.Add(New StructureItem("A01", "Arcade"))
MyBase.Add(New StructureItem("A02", "Autentica"))
MyBase.Add(New StructureItem("A03", "Belface"))
MyBase.Add(New StructureItem("A04", "Heering"))
End Sub
Public Overloads Sub Add(code As String, description As String)
MyBase.Add(New StructureItem(code, description))
End Sub
End Class
Public Class StructureItem
Implements INotifyPropertyChanged
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Private _code As String
Private _description As String
Public Sub New(ByVal first As String, ByVal last As String)
Me._code = first
Me._description = last
End Sub
Public Property Code() As String
Get
Return Me._code
End Get
Set(ByVal value As String)
Me._code = value
OnPropertyChanged("Code")
End Set
End Property
Public Property Description() As String
Get
Return Me._description
End Get
Set(ByVal value As String)
Me._description = value
OnPropertyChanged("Description")
End Set
End Property
Protected Sub OnPropertyChanged(ByVal name As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(name))
End Sub
End Class
End Namespace
code behind
Imports StructureItems.SList
Class MainWindow
Dim _item As New ItemList
Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
_item.Add("A05", "Updated_")
End Sub
End Class
You need to add a constructor to your mainwindow, and then in the constructor you need to set the DataContext :
Me.DataContext = Me
Or something like that. I am a C# programmer and have forgotten VB.
这篇关于通过绑定更新listview(绑定初学者)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!