双向绑定 Datagrid
Binding Datagrid both ways
我正在尝试将 List
数据绑定到 DataGrid
。
到目前为止,它似乎只能以一种方式工作 - 当我手动更改显示的 DataGrid
中输入的内容时,List
会更新,但反之亦然(DataGrid
当我在 List
).
中更改某些内容时不会更改
这是我的 DataGrid
:
<DataGrid x:Name="MyDataGrid" AutoGenerateColumns="false" ItemsSource="{Binding}">
<DataGrid.Columns>
<DataGridTextColumn x:Name="ColName" Binding="{Binding Name}" Header="Name" />
<DataGridTextColumn x:Name="ColProperty" Binding="{Binding MyProperty}" Header="My Property" />
</DataGrid.Columns>
</DataGrid>
我输入我的数据如下:
Public Class Res
Public Shared TableData As New List(Of DataItem)
End Class
Public Class DataItem
Public Property Name() As String
Public Property MyProperty() As String
End Class
Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
Dim entry As New DataItem
entry.Name = "Test Name"
entry.MyProperty = "Test Property"
Res.TableData.Add(entry)
Me.MyDataGrid.ItemsSource = Res.TableData
End Sub
但是,当我尝试更改我的数据时,通过执行以下操作:
Res.TableData.Item(0).Name = "Changed"
不起作用,DataGrid
中显示的值不会改变。
这是为什么?如何更新我的 DataGrid
?
如果有更好的方法将数据绑定到 DataGrid
,我愿意提出建议。
经过更多搜索,找到了一个有效的解决方案。
我做了什么,我补充说:
Me.MyDataGrid.Items.Refresh()
在我更改 List
中的值之后。
谢谢。
我正在尝试将 List
数据绑定到 DataGrid
。
到目前为止,它似乎只能以一种方式工作 - 当我手动更改显示的 DataGrid
中输入的内容时,List
会更新,但反之亦然(DataGrid
当我在 List
).
这是我的 DataGrid
:
<DataGrid x:Name="MyDataGrid" AutoGenerateColumns="false" ItemsSource="{Binding}">
<DataGrid.Columns>
<DataGridTextColumn x:Name="ColName" Binding="{Binding Name}" Header="Name" />
<DataGridTextColumn x:Name="ColProperty" Binding="{Binding MyProperty}" Header="My Property" />
</DataGrid.Columns>
</DataGrid>
我输入我的数据如下:
Public Class Res
Public Shared TableData As New List(Of DataItem)
End Class
Public Class DataItem
Public Property Name() As String
Public Property MyProperty() As String
End Class
Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
Dim entry As New DataItem
entry.Name = "Test Name"
entry.MyProperty = "Test Property"
Res.TableData.Add(entry)
Me.MyDataGrid.ItemsSource = Res.TableData
End Sub
但是,当我尝试更改我的数据时,通过执行以下操作:
Res.TableData.Item(0).Name = "Changed"
不起作用,DataGrid
中显示的值不会改变。
这是为什么?如何更新我的 DataGrid
?
如果有更好的方法将数据绑定到 DataGrid
,我愿意提出建议。
经过更多搜索,找到了一个有效的解决方案。
我做了什么,我补充说:
Me.MyDataGrid.Items.Refresh()
在我更改 List
中的值之后。
谢谢。