我的問題是我有一個選擇器,當我在另一個選擇器上選擇一個選項時它沒有填充。我查看了多種解決方案,但沒有作業。
XAML:
<Picker x:Name="customerPicker"
Title="Customer"
ItemsSource="{Binding CustomerSource}"
SelectedItem="{Binding SelectedCustomer}"
Grid.Row="0" Grid.Column="1" />
<Picker x:Name="salerOrderPicker"
Title="Sales Order"
ItemsSource="{Binding BuildSalesOrderSource}"
SelectedItem="{Binding SelectedSalesOrder}"
Grid.Row="0" Grid.Column="1" />
這是我的 Pickers 的 XAML 代碼。第一個選擇器正在填充。
視圖模型代碼:
public List<string> CustomerSource { get; set; }
public List<string> BuildSalesOrderSource { get; set; }
為選擇器宣告我的串列
private void GetSQLBuildCustomer()
{
try
{
var customerList = new List<string>();
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
using (SqlCommand command = new SqlCommand("SELECT CardCode, CardName FROM OCRD WHERE CardCode LIKE "
"'FOR001' OR CardCode LIKE '115638'", connection))
{
using (SqlDataReader reader = command.ExecuteReader())
{
if (reader != null)
{
while (reader.Read())
{
if (reader["CardName"] != DBNull.Value)
{
customerList.Add(new Item(reader.GetString(0), reader.GetString(1)).ToString());
}
}
CustomerSource = customerList;
//Application.Current.MainPage.BindingContext = CustomerSource;
}
}
}
connection.Close();
}
}
catch (Exception ex)
{
Application.Current.MainPage.DisplayAlert("Alert", "Error Customer Selection lists :" ex.Message, "OK");
}
}
這是為了填充第一個選擇器。這段代碼有效并填充了選擇器
public string selectedCustomer;
public string SelectedCustomer
{
get { return selectedCustomer; }
set
{
selectedCustomer = value;
OnPropertyChanged("SelectedCustomer");
if (selectedCustomer != null)
{
GetSQLBuildSalesOrders(selectedCustomer);
Static_Var.Changed = true;
}
}
}
這是我在第一個選擇器中選擇一個選項并且正在作業時
private void GetSQLBuildSalesOrders(string sCustomer)
{
try
{
var salesOrderList = new List<string>();
salesOrderList.Clear();
string Customer = string.Empty;
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
if (selectedCustomer != "")
{
string[] CustomerName = selectedCustomer.Split(':');
Customer = CustomerName[0];
}
string SQL = "";
if (Customer.ToUpper().Contains("FOR001"))
{
SQL = "SELECT TOP 1 DocNum, O.[DocEntry],R.[DocEntry], R.Dscription FROM [ORDR] O "
"INNER JOIN RDR1 R ON O.DocEntry = R.DocEntry "
"WHERE [CardCode] = '" Customer "' AND [DocStatus] = 'O'";
//" AND Cast(O.DocDate as date) = Cast(Getdate() as Date)";
}
else
{
SQL = "SELECT TOP 5 DocNum, O.[DocEntry],R.[DocEntry], R.Dscription FROM [ORDR] O "
"INNER JOIN RDR1 R ON O.DocEntry = R.DocEntry "
"WHERE [CardCode] = '" Customer "' AND [DocStatus] = 'O'";
}
using (SqlCommand command = new SqlCommand(SQL, connection))
{
using (SqlDataReader reader = command.ExecuteReader())
{
if (reader != null)
{
while (reader.Read())
{
salesOrderList.Add(reader.GetInt32(0).ToString() " : " reader.GetString(3));
DocEntrySelected = reader.GetInt32(1);
}
BuildSalesOrderSource = salesOrderList;
//Application.Current.MainPage.BindingContext = BuildSalesOrderSource;
}
}
}
connection.Close();
}
//BuildSalesOrderSource.Add(" ");
}
catch (Exception ex)
{
Application.Current.MainPage.DisplayAlert("Alert", "Error Creating Orders Selection lists for selected Customer: "
ex.Message, "OK");
}
}
這是第二個不更新它的選擇器填充串列但不顯示任何內容。
謝謝你的幫助。
uj5u.com熱心網友回復:
請更改BuildSalesOrderSourcefromList<string>到的型別ObservableCollection<string>。
換句話說,您可以替換以下代碼:
public List<string> BuildSalesOrderSource { get; set; }
帶代碼:
public ObservableCollection<string> BuildSalesOrderSource { get; set; } = new ObservableCollection<string>();
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/416180.html
標籤:
上一篇:單擊按鈕時更改按鈕名稱
