0

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 grid 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"/>
H.B.
  • 166,899
  • 29
  • 327
  • 400
Jack McG
  • 595
  • 2
  • 6
  • 5
  • Don't know if this matters bit I think the difference between TextBlock and Textbox is the box's default bind mode is TwoWay while it's oneWay for the Block. So if properties in a anonymous class are readonly, that could have sonmething to do with it. May I ask why you don't just create a class for the Vendor or use the Entity class Linq creates for you? – Ingó Vals Jul 29 '10 at 15:25
  • Got it fixed. The problem was in the LINQ statement. Rather than specifying a set of fields to use, I asked for all fields. That is, instead of using "select new {csv list of fields};" I used "select v;" the equivalent of a SQL Select * from xyz statement. Given that datacontext, there was no difference between using textblocks and textboxes. So I guess this question is self-answered. – Jack McG Jul 29 '10 at 15:29

1 Answers1

0

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

terphi
  • 781
  • 7
  • 18
  • I was thinking Silverlight, my mistake: http://stackoverflow.com/questions/2684954/silverlight-4-data-binding-with-anonymous-types – terphi Jan 26 '12 at 22:57