如何根据另一个选择器的选择填充选择器?
How to populate a picker depending on a choice from another picker?
我的问题是我有一个选择器,当我在另一个选择器上选择一个选项时它没有填充。我查看了多种解决方案,但没有一个有效。
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" />
这是我的选择器的 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;
}
}
}
这是我 select 第一个选择器中的一个选项并且正在工作的时候
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");
}
}
这是第二个选择器,它没有更新它填充列表但不显示任何内容。
感谢您的帮助。
请将 BuildSalesOrderSource
的类型从 List<string>
更改为 ObservableCollection<string>
。
换句话说,你可以替换下面的代码:
public List<string> BuildSalesOrderSource { get; set; }
使用代码:
public ObservableCollection<string> BuildSalesOrderSource { get; set; } = new ObservableCollection<string>();
我的问题是我有一个选择器,当我在另一个选择器上选择一个选项时它没有填充。我查看了多种解决方案,但没有一个有效。
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" />
这是我的选择器的 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;
}
}
}
这是我 select 第一个选择器中的一个选项并且正在工作的时候
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");
}
}
这是第二个选择器,它没有更新它填充列表但不显示任何内容。
感谢您的帮助。
请将 BuildSalesOrderSource
的类型从 List<string>
更改为 ObservableCollection<string>
。
换句话说,你可以替换下面的代码:
public List<string> BuildSalesOrderSource { get; set; }
使用代码:
public ObservableCollection<string> BuildSalesOrderSource { get; set; } = new ObservableCollection<string>();