【问题标题】:changing textblock to textbox loses data bindings -将文本块更改为文本框会丢失数据绑定 -
【发布时间】:2010-07-28 17:01:43
【问题描述】:

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

我复制了为绑定提供数据的 c# LINQ 代码和 XAML 代码,重新编译,将其保存到 NotePad++,然后关闭了 VS(8)。重新打开项目后,我重新插入了代码,但还是没有数据。

任何有帮助的想法将不胜感激。

这是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;

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"/>

【问题讨论】:

  • 不知道这是否重要我认为TextBlock和Textbox之间的区别在于框的默认绑定模式是TwoWay,而它是Block的oneWay。因此,如果匿名类中的属性是只读的,那可能与它有关。请问您为什么不只是为 Vendor 创建一个类或使用 Linq 为您创建的 Entity 类?
  • 解决了。问题出在 LINQ 语句中。我没有指定要使用的一组字段,而是要求提供所有字段。也就是说,而不是使用“select new {csv list of fields};”我用“选择v;”相当于 SQL Select * from xyz 语句。鉴于数据上下文,使用文本块和文本框之间没有区别。所以我想这个问题是自我回答的。

标签: c# wpf xaml data-binding


【解决方案1】:

您不能绑定到匿名类型,这就是在您开始选择整条记录后绑定起作用的原因。

【讨论】:

猜你喜欢
  • 2017-12-08
  • 2014-03-13
  • 1970-01-01
  • 2010-12-10
  • 1970-01-01
  • 1970-01-01
  • 2011-09-30
  • 2011-07-01
  • 1970-01-01
相关资源
最近更新 更多