概述: 我已經設置了ComoboBox綁定到List屬性。但是當我運行該應用程序時,組合框中沒有填充數據。如何解析ComboBox上的空綁定?
調試步驟:
- 我檢查了輸出窗口綁定錯誤,告訴我說,數據源可能爲空。
- 然後我在QueryList屬性的setter上設置一個斷點。這表明列表計數爲0.看起來在調用setter之後執行對init的調用。
我的想法是列表被調用setter後被初始化。這意味着在該階段綁定將在組合框上被調用時綁定將爲空。
問:
我怎樣才能調用init方法之前QueryList二傳手我的名單被稱爲?
代碼片段:
後面的代碼 -
//The binding property for the combo box
private List<string> _queryList;
public List<string> QueryList
{
get
{
return this._queryList;
}
set
{
this._queryList = value;
}
}
public MainWindow()
{
InitializeComponent();
// Establish the Login control
Ctrl = new CrmLogin();
QueryList = new List<string>();
InitQueryList();
}
//Call to init the list data
private void InitQueryList()
{
_queryList.Add("Query queues with unapproved email routers");
_queryList.Add("Query queues with emails in pending send status");
}
組合框結合設置 -
<ComboBox HorizontalAlignment="Left" ItemsSource="{Binding QueryList}" Grid.Column="1" x:Name="queryComboBox" Grid.Row="0" VerticalAlignment="Bottom" Width="300" Visibility="Collapsed" Text="Select a query"/>
你有沒有嘗試過在構造函數的末尾調用InitializeComponent()? – Breeze
嘗試使用ObservableCollection而不是列表 – Taterhead