2010-07-28 102 views
0

這是一個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"/> 
+0

哪一個還在工作? – Goblin 2010-07-28 17:11:57

+0

不知道這是否有點重要我認爲TextBlock和Textbox之間的區別是盒子的默認綁定模式是TwoWay,而它是Block的單向模式。所以如果一個匿名類中的屬性是隻讀的,那麼這可能與它有關。 請問您爲什麼不爲供應商創建一個類或使用Linq爲您創建的實體類? – 2010-07-29 15:25:07

+0

得到修復。問題出在LINQ聲明中。我沒有指定要使用的一組字段,而是詢問了所有字段。也就是 而不是使用「select new {csv list of fields};」我用「選擇v;」相當於一個SQL Select * from xyz語句。鑑於datacontext,使用文本塊和文本框沒有區別。 所以我想這個問題是自我回答。 – 2010-07-29 15:29:53

回答

0

您不能綁定到匿名類型,這就是爲什麼你開始選擇進行綁定工作整個記錄。

+0

這不是真的,你可以綁定到匿名類型就好... – 2012-01-24 17:14:25

+0

我在想Silverlight,我的錯誤:http://stackoverflow.com/questions/2684954/silverlight-4-data-binding-with-匿名類型 – terphi 2012-01-26 22:57:32