Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/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
C# 将textblock更改为textbox会丢失数据绑定-_C#_Wpf_Xaml_Data Binding - Fatal编程技术网

C# 将textblock更改为textbox会丢失数据绑定-

C# 将textblock更改为textbox会丢失数据绑定-,c#,wpf,xaml,data-binding,C#,Wpf,Xaml,Data Binding,这是一个wpf/c应用程序-我有一个网格,其中使用了文本框,数据绑定很好。后来我用textblocks构建了另一个网格。两者都很好。但是,当我将第二个网格中的文本块更改为文本框时,除了一个之外,所有的文本块都停止显示数据 <TextBox x:Name="txtVendorAddr1" Text="{Binding Path= Add

这是一个wpf/c应用程序-我有一个网格,其中使用了文本框,数据绑定很好。后来我用textblocks构建了另一个网格。两者都很好。但是,当我将第二个网格中的文本块更改为文本框时,除了一个之外,所有的文本块都停止显示数据

                                    <TextBox x:Name="txtVendorAddr1"
                                           Text="{Binding Path= Address1}" 
                                           Background="AliceBlue"
                                           Grid.Row="2"
                                           Grid.Column="1"/>

                                    <TextBox x:Name="txtVendorAddr2"
                                           Text="{Binding Path= Address2}"  
                                           Grid.Row="3"
                                           Grid.Column="1"
                                           Background="AliceBlue"/>
                                    <Label Content="City" 
                                       Grid.Row="4"
                                       Grid.Column="0"/>

                                    <TextBox x:Name="txtVendorCity"
                                       Text="{Binding Path= CSZ}"  
                                           Grid.Row="4"
                                       Grid.Column="1"/>

                                    <Label Content="Phone"
                                       Grid.Row="1"
                                       Grid.Column="0"/>
                                    <TextBox  x:Name="txtVendorPhone" 
                                            Text="{Binding Path = Phone}"
                                            Grid.Row="1"
                                            Grid.Column="1"/>

                                    <Label Content="Fax" 
                                       Grid.Column="2" 
                                        />
                                    <TextBox Text="{Binding Path = Fax}" 
                                           Grid.Row="0"
                                           Grid.Column="3"  />

                                    <Label Content="Terms" 
                                           Grid.Row="2"
                                           Grid.Column="2"/>
                                    <TextBox Text="{Binding Path = Terms}"
                                               Grid.Row="2"
                                           Grid.Column="3"/>

                                    <Label Content="Notes" 
                                           Grid.Row="3"
                                           Grid.Column="2" />
                                    <TextBox Text="{Binding Path = Notes}"
                                               Grid.Row="3"
                                               Grid.Column="3"
                                               Grid.RowSpan="2" 
                                             TextWrapping="WrapWithOverflow" />

                                    <Label Content="eMail" 
                                           Grid.Row="1"
                                           Grid.Column="2"  />
                                    <TextBox Text="{Binding Path = eMail}" 
                                               Grid.Row="1"
                                               Grid.Column="3"/>
我复制了为绑定提供数据的c LINQ代码和XAML代码,重新编译,保存到NotePad++,并关闭了VS8。重新打开项目后,我重新插入了代码,但仍然没有得到任何数据

                                    <TextBox x:Name="txtVendorAddr1"
                                           Text="{Binding Path= Address1}" 
                                           Background="AliceBlue"
                                           Grid.Row="2"
                                           Grid.Column="1"/>

                                    <TextBox x:Name="txtVendorAddr2"
                                           Text="{Binding Path= Address2}"  
                                           Grid.Row="3"
                                           Grid.Column="1"
                                           Background="AliceBlue"/>
                                    <Label Content="City" 
                                       Grid.Row="4"
                                       Grid.Column="0"/>

                                    <TextBox x:Name="txtVendorCity"
                                       Text="{Binding Path= CSZ}"  
                                           Grid.Row="4"
                                       Grid.Column="1"/>

                                    <Label Content="Phone"
                                       Grid.Row="1"
                                       Grid.Column="0"/>
                                    <TextBox  x:Name="txtVendorPhone" 
                                            Text="{Binding Path = Phone}"
                                            Grid.Row="1"
                                            Grid.Column="1"/>

                                    <Label Content="Fax" 
                                       Grid.Column="2" 
                                        />
                                    <TextBox Text="{Binding Path = Fax}" 
                                           Grid.Row="0"
                                           Grid.Column="3"  />

                                    <Label Content="Terms" 
                                           Grid.Row="2"
                                           Grid.Column="2"/>
                                    <TextBox Text="{Binding Path = Terms}"
                                               Grid.Row="2"
                                           Grid.Column="3"/>

                                    <Label Content="Notes" 
                                           Grid.Row="3"
                                           Grid.Column="2" />
                                    <TextBox Text="{Binding Path = Notes}"
                                               Grid.Row="3"
                                               Grid.Column="3"
                                               Grid.RowSpan="2" 
                                             TextWrapping="WrapWithOverflow" />

                                    <Label Content="eMail" 
                                           Grid.Row="1"
                                           Grid.Column="2"  />
                                    <TextBox Text="{Binding Path = eMail}" 
                                               Grid.Row="1"
                                               Grid.Column="3"/>
任何有帮助的想法都将不胜感激

                                    <TextBox x:Name="txtVendorAddr1"
                                           Text="{Binding Path= Address1}" 
                                           Background="AliceBlue"
                                           Grid.Row="2"
                                           Grid.Column="1"/>

                                    <TextBox x:Name="txtVendorAddr2"
                                           Text="{Binding Path= Address2}"  
                                           Grid.Row="3"
                                           Grid.Column="1"
                                           Background="AliceBlue"/>
                                    <Label Content="City" 
                                       Grid.Row="4"
                                       Grid.Column="0"/>

                                    <TextBox x:Name="txtVendorCity"
                                       Text="{Binding Path= CSZ}"  
                                           Grid.Row="4"
                                       Grid.Column="1"/>

                                    <Label Content="Phone"
                                       Grid.Row="1"
                                       Grid.Column="0"/>
                                    <TextBox  x:Name="txtVendorPhone" 
                                            Text="{Binding Path = Phone}"
                                            Grid.Row="1"
                                            Grid.Column="1"/>

                                    <Label Content="Fax" 
                                       Grid.Column="2" 
                                        />
                                    <TextBox Text="{Binding Path = Fax}" 
                                           Grid.Row="0"
                                           Grid.Column="3"  />

                                    <Label Content="Terms" 
                                           Grid.Row="2"
                                           Grid.Column="2"/>
                                    <TextBox Text="{Binding Path = Terms}"
                                               Grid.Row="2"
                                           Grid.Column="3"/>

                                    <Label Content="Notes" 
                                           Grid.Row="3"
                                           Grid.Column="2" />
                                    <TextBox Text="{Binding Path = Notes}"
                                               Grid.Row="3"
                                               Grid.Column="3"
                                               Grid.RowSpan="2" 
                                             TextWrapping="WrapWithOverflow" />

                                    <Label Content="eMail" 
                                           Grid.Row="1"
                                           Grid.Column="2"  />
                                    <TextBox Text="{Binding Path = eMail}" 
                                               Grid.Row="1"
                                               Grid.Column="3"/>
这是c代码

var vendDetail = from v in dbC.Vendors
                  where v.VendorID == vendorid
                   select new
                   {
                      v.Address1,
                      v.Address2,
                      CSZ = v.City + ", " + v.State + " " + v.Zip,
                      v.Phone,
                      v.Fax,
                      v.Contact,
                      v.Terms,
                      v.eMail
                    };

                grVendorData.DataContext = vendDetail;
                                    <TextBox x:Name="txtVendorAddr1"
                                           Text="{Binding Path= Address1}" 
                                           Background="AliceBlue"
                                           Grid.Row="2"
                                           Grid.Column="1"/>

                                    <TextBox x:Name="txtVendorAddr2"
                                           Text="{Binding Path= Address2}"  
                                           Grid.Row="3"
                                           Grid.Column="1"
                                           Background="AliceBlue"/>
                                    <Label Content="City" 
                                       Grid.Row="4"
                                       Grid.Column="0"/>

                                    <TextBox x:Name="txtVendorCity"
                                       Text="{Binding Path= CSZ}"  
                                           Grid.Row="4"
                                       Grid.Column="1"/>

                                    <Label Content="Phone"
                                       Grid.Row="1"
                                       Grid.Column="0"/>
                                    <TextBox  x:Name="txtVendorPhone" 
                                            Text="{Binding Path = Phone}"
                                            Grid.Row="1"
                                            Grid.Column="1"/>

                                    <Label Content="Fax" 
                                       Grid.Column="2" 
                                        />
                                    <TextBox Text="{Binding Path = Fax}" 
                                           Grid.Row="0"
                                           Grid.Column="3"  />

                                    <Label Content="Terms" 
                                           Grid.Row="2"
                                           Grid.Column="2"/>
                                    <TextBox Text="{Binding Path = Terms}"
                                               Grid.Row="2"
                                           Grid.Column="3"/>

                                    <Label Content="Notes" 
                                           Grid.Row="3"
                                           Grid.Column="2" />
                                    <TextBox Text="{Binding Path = Notes}"
                                               Grid.Row="3"
                                               Grid.Column="3"
                                               Grid.RowSpan="2" 
                                             TextWrapping="WrapWithOverflow" />

                                    <Label Content="eMail" 
                                           Grid.Row="1"
                                           Grid.Column="2"  />
                                    <TextBox Text="{Binding Path = eMail}" 
                                               Grid.Row="1"
                                               Grid.Column="3"/>
以及XAML代码:

                                    <TextBox x:Name="txtVendorAddr1"
                                           Text="{Binding Path= Address1}" 
                                           Background="AliceBlue"
                                           Grid.Row="2"
                                           Grid.Column="1"/>

                                    <TextBox x:Name="txtVendorAddr2"
                                           Text="{Binding Path= Address2}"  
                                           Grid.Row="3"
                                           Grid.Column="1"
                                           Background="AliceBlue"/>
                                    <Label Content="City" 
                                       Grid.Row="4"
                                       Grid.Column="0"/>

                                    <TextBox x:Name="txtVendorCity"
                                       Text="{Binding Path= CSZ}"  
                                           Grid.Row="4"
                                       Grid.Column="1"/>

                                    <Label Content="Phone"
                                       Grid.Row="1"
                                       Grid.Column="0"/>
                                    <TextBox  x:Name="txtVendorPhone" 
                                            Text="{Binding Path = Phone}"
                                            Grid.Row="1"
                                            Grid.Column="1"/>

                                    <Label Content="Fax" 
                                       Grid.Column="2" 
                                        />
                                    <TextBox Text="{Binding Path = Fax}" 
                                           Grid.Row="0"
                                           Grid.Column="3"  />

                                    <Label Content="Terms" 
                                           Grid.Row="2"
                                           Grid.Column="2"/>
                                    <TextBox Text="{Binding Path = Terms}"
                                               Grid.Row="2"
                                           Grid.Column="3"/>

                                    <Label Content="Notes" 
                                           Grid.Row="3"
                                           Grid.Column="2" />
                                    <TextBox Text="{Binding Path = Notes}"
                                               Grid.Row="3"
                                               Grid.Column="3"
                                               Grid.RowSpan="2" 
                                             TextWrapping="WrapWithOverflow" />

                                    <Label Content="eMail" 
                                           Grid.Row="1"
                                           Grid.Column="2"  />
                                    <TextBox Text="{Binding Path = eMail}" 
                                               Grid.Row="1"
                                               Grid.Column="3"/>

                                    <TextBox x:Name="txtVendorAddr1"
                                           Text="{Binding Path= Address1}" 
                                           Background="AliceBlue"
                                           Grid.Row="2"
                                           Grid.Column="1"/>

                                    <TextBox x:Name="txtVendorAddr2"
                                           Text="{Binding Path= Address2}"  
                                           Grid.Row="3"
                                           Grid.Column="1"
                                           Background="AliceBlue"/>
                                    <Label Content="City" 
                                       Grid.Row="4"
                                       Grid.Column="0"/>

                                    <TextBox x:Name="txtVendorCity"
                                       Text="{Binding Path= CSZ}"  
                                           Grid.Row="4"
                                       Grid.Column="1"/>

                                    <Label Content="Phone"
                                       Grid.Row="1"
                                       Grid.Column="0"/>
                                    <TextBox  x:Name="txtVendorPhone" 
                                            Text="{Binding Path = Phone}"
                                            Grid.Row="1"
                                            Grid.Column="1"/>

                                    <Label Content="Fax" 
                                       Grid.Column="2" 
                                        />
                                    <TextBox Text="{Binding Path = Fax}" 
                                           Grid.Row="0"
                                           Grid.Column="3"  />

                                    <Label Content="Terms" 
                                           Grid.Row="2"
                                           Grid.Column="2"/>
                                    <TextBox Text="{Binding Path = Terms}"
                                               Grid.Row="2"
                                           Grid.Column="3"/>

                                    <Label Content="Notes" 
                                           Grid.Row="3"
                                           Grid.Column="2" />
                                    <TextBox Text="{Binding Path = Notes}"
                                               Grid.Row="3"
                                               Grid.Column="3"
                                               Grid.RowSpan="2" 
                                             TextWrapping="WrapWithOverflow" />

                                    <Label Content="eMail" 
                                           Grid.Row="1"
                                           Grid.Column="2"  />
                                    <TextBox Text="{Binding Path = eMail}" 
                                               Grid.Row="1"
                                               Grid.Column="3"/>

您无法绑定到匿名类型,这就是为什么在您开始选择整个记录后绑定工作。

不知道这是否重要。我认为TextBlock和Textbox之间的区别在于,框的默认绑定模式是双向的,而块的默认绑定模式是单向的。所以,如果匿名类中的属性是只读的,那么这可能与此有关。我想问一下,为什么不为供应商创建一个类,或者使用Linq为您创建的实体类?已经修复了。问题出在LINQ声明中。我没有指定要使用的字段集,而是要求所有字段。也就是说,不使用select new{csv字段列表};我用selectv;相当于SQL Select*from xyz语句。考虑到datacontext,使用TextBlock和TextBox之间没有区别。所以我猜这个问题是自我回答的。我在想Silverlight,我的错误:
                                    <TextBox x:Name="txtVendorAddr1"
                                           Text="{Binding Path= Address1}" 
                                           Background="AliceBlue"
                                           Grid.Row="2"
                                           Grid.Column="1"/>

                                    <TextBox x:Name="txtVendorAddr2"
                                           Text="{Binding Path= Address2}"  
                                           Grid.Row="3"
                                           Grid.Column="1"
                                           Background="AliceBlue"/>
                                    <Label Content="City" 
                                       Grid.Row="4"
                                       Grid.Column="0"/>

                                    <TextBox x:Name="txtVendorCity"
                                       Text="{Binding Path= CSZ}"  
                                           Grid.Row="4"
                                       Grid.Column="1"/>

                                    <Label Content="Phone"
                                       Grid.Row="1"
                                       Grid.Column="0"/>
                                    <TextBox  x:Name="txtVendorPhone" 
                                            Text="{Binding Path = Phone}"
                                            Grid.Row="1"
                                            Grid.Column="1"/>

                                    <Label Content="Fax" 
                                       Grid.Column="2" 
                                        />
                                    <TextBox Text="{Binding Path = Fax}" 
                                           Grid.Row="0"
                                           Grid.Column="3"  />

                                    <Label Content="Terms" 
                                           Grid.Row="2"
                                           Grid.Column="2"/>
                                    <TextBox Text="{Binding Path = Terms}"
                                               Grid.Row="2"
                                           Grid.Column="3"/>

                                    <Label Content="Notes" 
                                           Grid.Row="3"
                                           Grid.Column="2" />
                                    <TextBox Text="{Binding Path = Notes}"
                                               Grid.Row="3"
                                               Grid.Column="3"
                                               Grid.RowSpan="2" 
                                             TextWrapping="WrapWithOverflow" />

                                    <Label Content="eMail" 
                                           Grid.Row="1"
                                           Grid.Column="2"  />
                                    <TextBox Text="{Binding Path = eMail}" 
                                               Grid.Row="1"
                                               Grid.Column="3"/>