开发者

changing textblock to textbox loses data bindings -

开发者 https://www.devze.com 2023-01-09 11:57 出处:网络
this is a wpf/c# app -I have one grid where I used text boxes and the data bindings were fine. I later built another 开发者_如何学Pythongrid using textblocks. both worked well. but when I changed the

this is a wpf/c# app - I have one grid where I used text boxes and the data bindings were fine. I later built another 开发者_如何学Pythongrid using textblocks. both worked well. but when I changed the textblocks in the second grid into textboxes, all but one stopped displaying data.

I copied both the c# LINQ code and the XAML code that provide the data for the bindings, recompiled, saved it to NotePad++, and closed VS(8). After reopening the project, I reinserted the code, but I still got no data.

Any ideas that would help would be greatly appreciated.

Here's the c# code

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;

And the XAML code:

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


You cannot bind to anonymous types, which is why the binding worked after you began selecting the whole record.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号