我创建了一个简单的数据网格来显示一些值,让用户更改它们并回读后台程序中更改的值。这是XAML设计文件
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button x:Name="ButAdd" Content="Add Row" HorizontalAlignment="Left" Height="23" Margin="362,34,0,0" VerticalAlignment="Top" Width="77"/>
<TextBox x:Name="TeBoResult" HorizontalAlignment="Left" Height="90" Margin="52,220,0,0" TextWrapping="Wrap" Text="Display the Row 0 colmn 1 changed value here:" VerticalAlignment="Top" Width="322" AcceptsReturn="True" IsManipulationEnabled="True"/>
<Button x:Name="ButRead" Content="Read Row 2" HorizontalAlignment="Left" Height="24" Margin="425,184,0,0" VerticalAlignment="Top" Width="82"/>
<DataGrid x:Name="DaGr" ItemsSource="{Binding}" HorizontalAlignment="Left" Height="133" Margin="10,75,0,0" VerticalAlignment="Top" Width="174" AutoGenerateColumns="False" IsManipulationEnabled="True" EnableColumnVirtualization="True" >
<DataGrid.Columns>
<DataGridTextColumn Header="Text" Binding="{Binding Path=No, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<DataGridCheckBoxColumn Header="Check box" Binding="{Binding Path=Sel}"/>
<DataGridComboBoxColumn Header="Combo box" Binding.XmlNamespaceManager="{Binding Path=Drop}"/>
</DataGrid.Columns>
</DataGrid>
<ComboBox x:Name="CoBo_IN" HorizontalAlignment="Left" Height="20" Margin="344,100,0,0" VerticalAlignment="Top" Width="84" Visibility="Visible">
<ComboBoxItem Content="Move" HorizontalAlignment="Left" Width="88"/>
<ComboBoxItem Content="Dock" HorizontalAlignment="Left" Width="88" Selected="ComboBoxItem_Selected"/>
</ComboBox>
</Grid>
背景vb.net代码在这里
Imports System.Collections.ObjectModel
Imports System.ComponentModel
Imports System.Runtime.CompilerServices
Imports DataGrid.datset
Imports System.Windows
Class MainWindow
Public Property coll As New ObservableCollection(Of bind)()
Private Sub ButAdd_Click(sender As Object, e As RoutedEventArgs) Handles ButAdd.Click
Dim qw As New bind()
qw.No = "Change Me"
qw.Sel = Nothing
qw.Drop = CoBo_IN
coll.Add(qw)
DaGr.ItemsSource = coll
End Sub
Private Sub ButRead_Click(sender As Object, e As RoutedEventArgs) Handles ButRead.Click
Dim val As String
For Each item As bind In DaGr.Items
val = item.No
TeBoResult.Text = TeBoResult.Text & val
Next
End Sub
End Class
Public Class datset : Implements INotifyPropertyChanged
Public Event PropertyChanged As PropertyChangedEventHandler _
Implements INotifyPropertyChanged.PropertyChanged
Private Sub NotifyPropertyChanged(<CallerMemberName()> Optional ByVal propertyName As String = Nothing)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub
Public Structure bind
Public Property No As String
Public Property Sel As Boolean
Public Property Drop As ComboBox
End Structure
End Class
所以当我点击Add Row按钮时,带有默认内容的行会被添加到TextColumn和CheckBoxColumn中,但是ComboBoxColumn dosent会显示组合!!(但是当我在这个单元格内双击时,ComboBox会出现,但它是空的)。这种行为可能是什么原因?
接下来,用户将更改TextColumn中的内容,并根据需要在GUI中进行更改。
接下来,当用户单击botton Read Row时,TextColumn的所有内容将依次读取并显示在Result文本框中。问题是,当GUI按顺序读取时有一个新文本, val 变量仍然只显示以前绑定的值。我认为问题在于TwoWay绑定,但它似乎是另一回事。
为什么 DataGrid 上的读取不会给出更新值?
答案 0 :(得分:0)
我不太了解VB,但我可以看到这里有些东西不行。例如ItemsSource="{Binding}"
。在这里,您必须引用要绑定的集合,在本例中为coll
。代码应该是这样的:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
x:Name="window">
<Grid>
<Button x:Name="ButAdd" Content="Add Row" HorizontalAlignment="Left" Height="23" Margin="362,34,0,0" VerticalAlignment="Top" Width="77"/>
<TextBox x:Name="TeBoResult" HorizontalAlignment="Left" Height="90" Margin="52,220,0,0" TextWrapping="Wrap" Text="Display the Row 0 colmn 1 changed value here:" VerticalAlignment="Top" Width="322" AcceptsReturn="True" IsManipulationEnabled="True"/>
<Button x:Name="ButRead" Content="Read Row 2" HorizontalAlignment="Left" Height="24" Margin="425,184,0,0" VerticalAlignment="Top" Width="82"/>
<DataGrid x:Name="DaGr" ItemsSource="{Binding col, ElementName =window}" HorizontalAlignment="Left" Height="133" Margin="10,75,0,0" VerticalAlignment="Top" Width="174" AutoGenerateColumns="False" IsManipulationEnabled="True" EnableColumnVirtualization="True" >
<DataGrid.Columns>
<DataGridTextColumn Header="Text" Binding="{Binding Path=No, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<DataGridCheckBoxColumn Header="Check box" Binding="{Binding Path=Sel, Mode=TwoWay}"/>
<DataGridComboBoxColumn Header="Combo box" Binding.XmlNamespaceManager="{Binding Path=Drop, Mode=TwoWay"/>
</DataGrid.Columns>
</DataGrid>
<ComboBox x:Name="CoBo_IN" HorizontalAlignment="Left" Height="20" Margin="344,100,0,0" VerticalAlignment="Top" Width="84" Visibility="Visible">
<ComboBoxItem Content="Move" HorizontalAlignment="Left" Width="88"/>
<ComboBoxItem Content="Dock" HorizontalAlignment="Left" Width="88" Selected="ComboBoxItem_Selected"/>
</ComboBox>
</Grid>
注意项目源绑定和窗口名称,还要注意绑定应该是双向的,也用于更新源代码。这个新绑定引用了col
集合。现在在他的代码背后,你不应该在每次添加项目时重新设置网格intems源。如果集合是一个可观察的集合,那么它会自动添加,现在这些项目应该可以工作::
Private Sub ButAdd_Click(sender As Object, e As RoutedEventArgs) Handles ButAdd.Click
Dim qw As New bind()
qw.No = "Change Me"
qw.Sel = Nothing
qw.Drop = CoBo_IN
coll.Add(qw)
End Sub