嘗試第一次數據綁定,但有些不起作用。我正試圖用此數據庫列中的項目列表填充組合框。我的數據集ds顯示它裏面有東西(調試器顯示它的計數大約是1500,應該在那裏的項數),如果我用一個閱讀器使用相同的連接字符串,我可以打印出所有應該出現在組合框中的東西。雖然組合框是空的,任何想法?爲什麼我的數據綁定工作不正常?
<ComboBox Height="23" HorizontalAlignment="Left" Margin="24,318,0,0" Name="comboBox2" VerticalAlignment="Top" Width="190" IsEditable="True" >
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding clt}" Width="100" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
而且現在的C#
DataSet ds = new DataSet();
string con_string = "user id=sql;password=pass;Server=xxx.xxx.xxx.xxx,xxxx;Database=db;Trusted_Connection=False;connection timeout=15";
string command_string = "SELECT clt FROM Client";
SqlConnection sql_con = new SqlConnection(con_string);
SqlCommand command = new SqlCommand(command_string, sql_con);
SqlDataAdapter sqlDa = new SqlDataAdapter();
sqlDa.SelectCommand = command;
sqlDa.Fill(ds);
comboBox2.DataContext = ds.Tables[0].DefaultView;
所有的代碼執行沒有任何異常。
嘗試'combobox2.ItemsSource = ds.Tables [0] .DefaultView',同時確保每個項目的綁定具有'clt' proeprty,基本上嘗試以下綁定文本框並告訴我們顯示的是什麼'文本=「{Binding}」' – sll 2012-02-06 10:10:04