这是一个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"/>
哪一个还在工作? – Goblin 2010-07-28 17:11:57
不知道这是否有点重要我认为TextBlock和Textbox之间的区别是盒子的默认绑定模式是TwoWay,而它是Block的单向模式。所以如果一个匿名类中的属性是只读的,那么这可能与它有关。 请问您为什么不为供应商创建一个类或使用Linq为您创建的实体类? – 2010-07-29 15:25:07
得到修复。问题出在LINQ声明中。我没有指定要使用的一组字段,而是询问了所有字段。也就是 而不是使用“select new {csv list of fields};”我用“选择v;”相当于一个SQL Select * from xyz语句。鉴于datacontext,使用文本块和文本框没有区别。 所以我想这个问题是自我回答。 – 2010-07-29 15:29:53