Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 从DataGrid读取的数据不工作_Wpf_Vb.net_Visual Studio 2012_Datagrid - Fatal编程技术网

Wpf 从DataGrid读取的数据不工作

Wpf 从DataGrid读取的数据不工作,wpf,vb.net,visual-studio-2012,datagrid,Wpf,Vb.net,Visual Studio 2012,Datagrid,我创建了一个简单的datagrid来显示一些值,让用户更改它们,并在后台程序中读回更改后的值。这是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="

我创建了一个简单的datagrid来显示一些值,让用户更改它们,并在后台程序中读回更改后的值。这是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
导入System.Collections.ObjectModel
导入System.ComponentModel
导入System.Runtime.CompilerServices
导入DataGrid.datset
导入系统.Windows
类主窗口
公共财产作为新的可观察集合(绑定)()
私有子ButAdd\u单击(发件人作为对象,e作为路由EventTargets)处理ButAdd。单击
将qw设置为新绑定()
qw.No=“改变我”
qw.Sel=无
qw.Drop=CoBo_IN
集合添加(qw)
DaGr.ItemsSource=coll
端接头
私有子ButRead\u单击(发件人作为对象,e作为路由EventTargets)处理ButRead。单击
作为字符串的Dim val
对于DaGr.Items中绑定的每个项目
val=项目编号
TeBoResult.Text=TeBoResult.Text&val
下一个
端接头
末级
公共类数据集:实现INotifyPropertyChanged
公共事件属性更改为PropertyChangedEventHandler_
实现INotifyPropertyChanged.PropertyChanged
私有子NotifyPropertyChanged(可选的ByVal propertyName作为字符串=无)
RaiseEvent PropertyChanged(Me,新PropertyChangedEventArgs(propertyName))
端接头
公共结构约束
公共属性不作为字符串
布尔型公共属性
公共属性作为组合框放置
端部结构
末级
因此,当我单击AddRow按钮时,带有默认内容的行被添加到TextColumn和CheckBoxColumn中,但是ComboxColumn不显示combo!!(但当我在该单元格内双击时,会出现组合框,但它是空的)。这种行为的原因是什么

接下来,用户将更改TextColumn中的内容,并根据需要在GUI中进行更改

接下来,当用户单击botton Read行时,TextColumn的所有内容都会依次读取,并显示在结果文本框中。问题是,尽管GUI在按顺序读取时有一个新文本,val变量仍然只显示以前绑定的值。我认为问题在于双向绑定,但似乎是另外一回事


为什么数据网格上的read没有给出更新的值?

我对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>

一个问题是,您试图双向绑定到
结构。如果您想要可写属性,请将其更改为
class
。@Shoe但该结构具有类型为“property”的变量,基本上是“get”和“set”,因此它不应该具有读写功能吗?@Shoe感谢您的建议,它工作正常,但在我进入编辑模式之前,甚至在进入编辑模式之后,组合框都不可见组合框是空的!!你知道为什么吗?你可以写结构,但它会创建一个赋值的副本。这显然会破坏绑定,因为绑定没有指向副本。就组合框而言,我看不出您在哪里为它设置itemssource。还不清楚为什么不使用
列表
类型在XAML文件中保存
DataGridCombobox
@Shoe的数据“DataGridCombobox”绑定到“Path=Drop”。“coll”有一个属性“combobox”,我正在将它设置为一个已经存在的combobox“CoBo_IN”,所以当我们将一个已经存在的变量关联到新变量时,这些项不应该被绑定吗?好的,我试过了,我看到现在我不需要使用Itemsource链接“DataGrid”,但组合框项目仍然是空的,当我单击“读取按钮”时,它仍然无法从网格中读取。
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