我创建了List allPackages。该列表实际上是包列表的列表。我需要能够单击主应用程序的ComboBox中的项目,从组合框列表中选择一个状态。选择状态后,如果List allPackages中包含包的包列表,我需要列表中每个包的Id字段显示在文本块或UI中的其他类型的工具中。当我尝试过,我没有成功地将访问的数据字段显示在UI中的任何类型的输出中。
我正在调查正在发生的事情,但我现在几乎陷入困境。就像当前运行的线程不允许我打印数据一样。当然,这必须是因为没有为某个类型的事件定义的方法,该方法在从ComboBox中选择项目时进行处理。我指的ComboBox是PkgsByStateCmbo。我几乎可以肯定这是事实。 从发布的代码中,您将看到每个Package对象都有一个地址,Id,州,zip和一个城市。在UI中单击按钮并完成字段并验证输入后,可以将包添加到基于要添加的包的状态字段的包列表中。这给了我基于状态的列表。现在看,我确定我应该在代码中使用Linq语句而不是嵌套列表,但我仍然只在1列表中处于当前状态。
/** Package Class */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CSC470_Lab6
{
class Package
{
public string Id { get; set; }
public string address { get; set;}
public string state { get; set; }
public int zip { get; set; }
public string city { get; set; }
}
}
/ **控制器类* /
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace CSC470_Lab6
{
class Controller
{
public List<List<Package>> allPackages = new List<List<Package>>();
public string alI = "0";
public string flI = "1";
public string gaI = "2";
public string kyI = "3";
public string miI = "4";
public string ncI = "5";
public string scI = "6";
public string tnI = "7";
public string wvI = "8";
public string vaI = "9";
public int currentPkgCount = 0;
public int initId = 10000;
/**
* Alabama, Florida, Georgia, Kentucky, Mississippi, North Carolina, South Carolina, Tennessee, West Virginia or Virginia.
* */
public List<Package> al = new List<Package>();
public List<Package> fl = new List<Package>();
public List<Package> ga = new List<Package>();
public List<Package> ky = new List<Package>();
public List<Package> mi = new List<Package>();
public List<Package> nc = new List<Package>();
public List<Package> sc = new List<Package>();
public List<Package> tn = new List<Package>();
public List<Package> wv = new List<Package>();
public List<Package> va = new List<Package>();
public void initMasterList()
{
allPackages.Add(al);
allPackages.Add(fl);
allPackages.Add(ga);
allPackages.Add(ky);
allPackages.Add(mi);
allPackages.Add(nc);
allPackages.Add(sc);
allPackages.Add(tn);
allPackages.Add(wv);
allPackages.Add(va);
}
public Boolean masterListHasContent()
{
Boolean result = false;
for (int i = 0; i < allPackages.Count; i++)
{
List<Package> temp = allPackages[i];
if (temp[i] != null)
{
result = true;
break;
}
}
return result;
}
public string[] getIds(List<Package> list)
{
string[] ids = null;
for (int i = 0; i < list.Count; i++)
{
ids[i] = list[i].Id;
}
return ids;
}
public List<Package> getStateList(int state)
{
return allPackages[state];
}
public void setInitId(){
initId++;
}
public int getInitId()
{
return initId;
}
}
}
/ ** MainWindow.xam.cs * /
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace CSC470_Lab6
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
string[] states = new string[] { "AL", "FL", "GA", "KY", "MI", "NC", "SC", "TN", "WV", "VA" };
Package theCurrentPackage;
Controller controller = new Controller();
public int zipcode;
public MainWindow()
{
InitializeComponent();
controller.initMasterList();
}
private void PkgsByStateCmbo_Click(object sender, RoutedEventArgs e)
{
if (PkgsByStateCmbo.SelectedIndex >= 0)
{
int index = PkgsByStateCmbo.SelectedIndex;
List<Package> list = controller.getStateList(index);
foreach (Package pkg in list)
{
ByStateBlk.Text = pkg.Id + "\n";
}
}
}
private void ScanBtn_Click(object sender, RoutedEventArgs e)
{
theCurrentPackage = new Package();
PkgIdBlk.IsEnabled = true;
controller.setInitId();
controller.getInitId();
PkgIdBlk.Text = controller.getInitId().ToString();
theCurrentPackage.Id = controller.getInitId().ToString();
setPackageAddress();
setCity();
setState();
setZip();
AddBtn.IsEnabled = true;
ErrorViewer.IsEnabled = true;
ScanBtn.IsEnabled = false;
}
public int getZip()
{
zipcode = int.Parse(ZipTxt.Text);
return zipcode;
}
public void setZip()
{
ZipTxt.Text = "";
ZipTxt.IsEnabled = true;
}
public string getState()
{
int index;
index = StateCmbo.SelectedIndex - 1;
if (index >= 0)
return states[index];
else
return "NVS";
}
public void setState()
{
StateCmbo.IsEnabled = true;
}
public void setCity()
{
CityTxt.Text = "";
CityTxt.IsEnabled = true;
}
public string getCity()
{
return CityTxt.Text;
}
public void setPackageAddress()
{
AddressTxt.Text = "";
AddressTxt.IsEnabled = true;
}
public string getPackageAddress()
{
return AddressTxt.Text;
}
private void AddBtn_Click(object sender, RoutedEventArgs e)
{
ScanBtn.IsEnabled = false;
bool addr = checkAddress();
bool cty = checkCity();
bool ckst = checkState();
bool chdz = checkZip();
if((addr == true)&&(cty == true)&&(ckst == true)&&(chdz == true))
{
theCurrentPackage.address = getPackageAddress();
theCurrentPackage.city = getCity();
theCurrentPackage.zip = getZip();
theCurrentPackage.state = getState();
ErrorViewer.Content = "Package added";
int index = StateCmbo.SelectedIndex - 1;
controller.allPackages[index].Add(theCurrentPackage);
theCurrentPackage = null;
AddressTxt.Text = "";
PkgIdBlk.Text = "";
CityTxt.Text = "";
ZipTxt.Text = "";
ScanBtn.IsEnabled = true;
StateCmbo.SelectedIndex = -1;
StateCmbo.IsEnabled = false;
PkgsByStateCmbo.IsEnabled = true;
}
else
{
ErrorViewer.Content = "Package add unsuccessful. Please check for errors and try again";
PkgIdBlk.Text = "";
AddressTxt.Text = "";
CityTxt.Text = "";
StateCmbo.SelectedIndex = -1;
ZipTxt.Text = "";
AddBtn.IsEnabled = false;
ScanBtn.IsEnabled = true;
theCurrentPackage = null;
}
}
public bool checkState()
{
bool result = false;
if (getState() != "NVS")
{
result = true;
}
return result;
}
public bool checkAddress()
{
bool result = false;
string str = getPackageAddress();
if (str != null)
result = true;
return result;
}
public bool checkCity()
{
bool result = false;
string str = getCity();
if (str != null)
result = true;
return result;
}
public bool checkZip()
{
bool result = false;
char[] zip = zipcode.ToString().ToCharArray();
for (int i = 0; i < zip.Length; i++)
{
int val = (int)Char.GetNumericValue(zip[i]);
if ((val.Equals(0)) || (val.Equals(1)) || (val.Equals(2)) || (val.Equals(3)) || (val.Equals(4))|| (val.Equals(5)) || (val.Equals(6))|| (val.Equals(7))|| (val.Equals(8)) || (val.Equals(9)))
{
result = true;
//return result;
}
else
{
result = false;
}
}
return result;
}
}
}
<Window x:Class="CSC470_Lab6.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid IsEnabled="True">
<Button x:Name="NextBtn" Content="Next >" HorizontalAlignment="Left" VerticalAlignment="Top" Width="71" Margin="315,235,0,0" IsEnabled="False"/>
<Button x:Name="ExitBtn" Content="Exit" HorizontalAlignment="Left" VerticalAlignment="Top" Width="44" Margin="266,235,0,0"/>
<Button x:Name="RemoveBtn" Content="Remove" HorizontalAlignment="Left" VerticalAlignment="Top" Width="60" Margin="201,235,0,0" IsEnabled="False"/>
<Button x:Name="AddBtn" Content="Add" HorizontalAlignment="Left" VerticalAlignment="Top" Width="48" Margin="148,234,0,0" IsEnabled="False" Click="AddBtn_Click"/>
<Button x:Name="ScanBtn" Content="Scan New" HorizontalAlignment="Left" VerticalAlignment="Top" Width="72" Margin="71,234,0,0" Click="ScanBtn_Click"/>
<Button x:Name="BackBtn" Content="< Back" HorizontalAlignment="Left" Margin="10,234,0,0" VerticalAlignment="Top" Width="56" IsEnabled="False"/>
<Grid x:Name="InnerGrid" HorizontalAlignment="Left" Height="135" Margin="10,94,0,0" VerticalAlignment="Top" Width="376">
<Label x:Name="PackageIdLbl" Content="Package ID:" HorizontalAlignment="Left" Margin="0,14,0,0" VerticalAlignment="Top" IsEnabled="False"/>
<Label x:Name="AddressLbl" Content="Address" HorizontalAlignment="Left" Margin="0,52,0,0" VerticalAlignment="Top" IsEnabled="False"/>
<Label x:Name="CityLbl" Content="City" HorizontalAlignment="Left" Margin="0,93,0,0" VerticalAlignment="Top" IsEnabled="False"/>
<Label x:Name="StateLbl" Content="State" HorizontalAlignment="Left" Margin="207,95,0,0" VerticalAlignment="Top" IsEnabled="False"/>
<Label x:Name="ZipLbl" Content="Zip" HorizontalAlignment="Left" Margin="286,95,0,0" VerticalAlignment="Top" IsEnabled="False"/>
<TextBox x:Name="CityTxt" HorizontalAlignment="Left" Height="23" Margin="87,99,0,0" TextWrapping="Wrap" Text="City_Text" VerticalAlignment="Top" Width="120" HorizontalContentAlignment="Center" IsEnabled="False"/>
<TextBox x:Name="ZipTxt" HorizontalAlignment="Left" Height="23" Margin="318,97,0,0" TextWrapping="Wrap" Text="00000" VerticalAlignment="Top" Width="48" HorizontalContentAlignment="Center" IsEnabled="False"/>
<ComboBox x:Name="StateCmbo" HorizontalAlignment="Left" Margin="244,98,0,0" VerticalAlignment="Top" Width="42" RenderTransformOrigin="1.614,0.47" IsEnabled="False">
<ComboBoxItem x:Name="noSelection" Content=""/>
<ComboBoxItem x:Name="al" Content="AL"/>
<ComboBoxItem x:Name="fl" Content="FL"/>
<ComboBoxItem x:Name="ga" Content="GA"/>
<ComboBoxItem x:Name="ky" Content="KY"/>
<ComboBoxItem x:Name="mi" Content="MI"/>
<ComboBoxItem x:Name="nc" Content="NC"/>
<ComboBoxItem x:Name="sc" Content="SC"/>
<ComboBoxItem x:Name="tn" Content="TN"/>
<ComboBoxItem x:Name="wv" Content="WV"/>
<ComboBoxItem x:Name="va" Content="VA"/>
</ComboBox>
<TextBlock x:Name="PkgIdBlk" HorizontalAlignment="Left" Margin="87,19,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="279" />
<TextBox x:Name="AddressTxt" HorizontalAlignment="Left" Height="23" Margin="87,53,0,0" TextWrapping="Wrap" Text="Address_Text" VerticalAlignment="Top" Width="279" HorizontalContentAlignment="Center" IsEnabled="False"/>
</Grid>
<GroupBox x:Name="PkgInfoGrp" Header="Package Information				" HorizontalAlignment="Left" Margin="10,95,0,0" VerticalAlignment="Top" Height="140" Width="376" IsEnabled="True"/>
<Label x:Name="ArrivedAtLbl" Content="Arrived At:" HorizontalAlignment="Left" Margin="10,63,0,0" VerticalAlignment="Top" Width="69" IsEnabled="False"/>
<TextBlock x:Name="ArrivedAtTxtBlk" HorizontalAlignment="Left" Margin="99,68,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="130" />
<GroupBox x:Name="PkgsByStateGrp" Header="Packages by state" HorizontalAlignment="Left" Margin="391,94,0,0" VerticalAlignment="Top" Height="160" Width="117" IsEnabled="True">
<Grid HorizontalAlignment="Left" Height="100" Margin="0,38,-3,-0.4" VerticalAlignment="Top" Width="107">
<TextBlock x:Name="ByStateBlk" HorizontalAlignment="Left" Margin="0,10,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="90" Width="107" Text="{Binding PkgsByStateCmbo, UpdateSourceTrigger=PropertyChanged}"/>
</Grid>
</GroupBox>
<ComboBox x:Name="PkgsByStateCmbo" HorizontalAlignment="Left" Margin="420,122,0,0" VerticalAlignment="Top" Width="65" IsReadOnly="True" Height="23" IsEnabled="True" SelectedItem="{Binding allPackages[SelectedIndex].List.state, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">
<ComboBoxItem Content=""/>
<ComboBoxItem Content="AL"/>
<ComboBoxItem Content="FL"/>
<ComboBoxItem Content="GA"/>
<ComboBoxItem Content="KY"/>
<ComboBoxItem Content="MI"/>
<ComboBoxItem Content="NC"/>
<ComboBoxItem Content="SC"/>
<ComboBoxItem Content="TN"/>
<ComboBoxItem Content="WV"/>
<ComboBoxItem Content="VA"/>
</ComboBox>
<ScrollViewer x:Name="ErrorViewer" HorizontalAlignment="Left" Height="47" Margin="10,264,0,0" VerticalAlignment="Top" Width="498"/>
</Grid>
</Window>
答案 0 :(得分:0)
让我先说一下,我没有示例Package类的内容,因此它基于假设。而我认为他们有名称和ID属性。除此之外,我还假设他们有独特的名字和id。这就是我使用FirstOrDefault()的原因。
我会做以下事情:
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e){
//Get the selected combobox item
ComboBoxItem selectedItem = (String)((ComboBox)sender).SelectedItem;
//Get the string out of the combobox item
String state = selectedItem.Content;
//Get the packageId from the allpackages list
Int64 packageId = (allPackages.Where(p => p.package.state.equals(state)).FirstOrDefault()).Id;
//Put the number in the textbox
PkgIdBlk.Text = packageId.ToString();
}
@Edit 13:30 22-07-2015
我读到有一个包列表的列表。 (这将有助于获得包类。)在这种情况下,如果你想要一个id列表,将适用以下内容:
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e){
//Get the selected combobox item
ComboBoxItem selectedItem = (String)((ComboBox)sender).SelectedItem;
//Get the string out of the combobox item
String state = selectedItem.Content;
//Get the list of packages from the allpackages list where the name applies
List<Package> packages = allPackages.Where(p => p.package.state.equals(state)).ToList();
//create a string to fill with id's
String idList = "";
//Loop to the prior selected list to fill the string with id's
foreach(Package p in packages){
idList += p.ID + ", ";
}
//Put the numberlist in the textbox
PkgIdBlk.Text = idList;
}
(您也可以使用StringBuilder类。但为了简单起见,我使用了String。)
@edit 22:34 23-07-2015 编辑了现在可用的包类的答案。 (按州名称替换)
文本框为空的原因可能有多种原因,这里有三个;
String状态为空,由于某种原因没有使用SelectedItem.Content填充。这导致Linq查询没有结果。
Linq(where)查询没有结果。这意味着foreach循环不会向字符串idList添加任何内容? (请记住,equals语句区分大小写。)
将idList字符串设置为错误的文本框?
@edit 22:43 23-07-2015 我看到一个问题,这个:
ComboBoxItem selectedItem = (String)((ComboBox)sender).SelectedItem;
应该是这样的:
ComboBoxItem selectedItem = (ComboBoxItem)((ComboBox)sender).SelectedItem;
答案 1 :(得分:0)
更正代码如下,我还评论说我将原始TextBlock更改为ListBox,这是解决方案的一部分。我还必须在PkgsByStateCombo_SelectionChanged方法中添加一个内部循环。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace CSC470_Lab6
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
int startingIndex = 0;
string[] states = new string[] { "AL", "FL", "GA", "KY", "MI", "NC", "SC", "TN", "WV", "VA" };
Package theCurrentPackage;
Controller controller = new Controller();
List<Package> newList = new List<Package>();
public int zipcode;
public MainWindow()
{
InitializeComponent();
PkgsByStateCmbo.ItemsSource = states;
controller.initMasterList();
for (int i = 0; i < controller.allPackages.Count; i++)
{
List<Package> lst = controller.allPackages[i];
lst.Clear();
}
}
private void ScanBtn_Click(object sender, RoutedEventArgs e)
{
ArrivedAtTxtBlk.IsEnabled = true;
theCurrentPackage = new Package();
PkgIdBlk.IsEnabled = true;
controller.setInitId();
controller.getInitId();
PkgIdBlk.Text = controller.getInitId().ToString();
theCurrentPackage.Id = controller.getInitId().ToString();
setPackageAddress();
setCity();
setState();
setZip();
AddBtn.IsEnabled = true;
ErrorViewer.IsEnabled = true;
ScanBtn.IsEnabled = false;
DateTime time = DateTime.Now;
ArrivedAtTxtBlk.Text = time.ToString();
}
public int getZip()
{
zipcode = int.Parse(ZipTxt.Text);
return zipcode;
}
public void setZip()
{
ZipTxt.Text = "";
ZipTxt.IsEnabled = true;
}
public string getState()
{
int index;
index = StateCmbo.SelectedIndex - 1;
if (index >= 0)
return states[index];
else
return "NVS";
}
public void setState()
{
StateCmbo.IsEnabled = true;
}
public void setCity()
{
CityTxt.Text = "";
CityTxt.IsEnabled = true;
}
public string getCity()
{
return CityTxt.Text;
}
public void setPackageAddress()
{
AddressTxt.Text = "";
AddressTxt.IsEnabled = true;
}
public string getPackageAddress()
{
return AddressTxt.Text;
}
private void AddBtn_Click(object sender, RoutedEventArgs e)
{
ScanBtn.IsEnabled = false;
bool addr = checkAddress();
bool cty = checkCity();
bool ckst = checkState();
bool chdz = checkZip();
if((addr == true)&&(cty == true)&&(ckst == true)&&(chdz == true))
{
theCurrentPackage.address = getPackageAddress();
theCurrentPackage.city = getCity();
theCurrentPackage.zip = getZip();
theCurrentPackage.state = getState();
ErrorViewer.Content = "Package added";
int index = StateCmbo.SelectedIndex - 1;
controller.allPackages[index].Add(theCurrentPackage);
//newList.Add(theCurrentPackage);
//theCurrentPackage = null;
AddressTxt.Text = "";
PkgIdBlk.Text = "";
CityTxt.Text = "";
ZipTxt.Text = "";
ScanBtn.IsEnabled = true;
StateCmbo.SelectedIndex = -1;
StateCmbo.IsEnabled = false;
PkgsByStateCmbo.IsEnabled = true;
NextBtn.IsEnabled = true;
BackBtn.IsEnabled = true;
}
else
{
ErrorViewer.Content = "Package add unsuccessful. Please check for errors and try again";
PkgIdBlk.Text = "";
AddressTxt.Text = "";
CityTxt.Text = "";
StateCmbo.SelectedIndex = -1;
ZipTxt.Text = "";
AddBtn.IsEnabled = false;
ScanBtn.IsEnabled = true;
theCurrentPackage = null;
}
}
public bool checkState()
{
bool result = false;
if (getState() != "NVS")
{
result = true;
}
return result;
}
public bool checkAddress()
{
bool result = false;
string str = getPackageAddress();
if (str != null)
result = true;
return result;
}
public bool checkCity()
{
bool result = false;
string str = getCity();
if (str != null)
result = true;
return result;
}
public bool checkZip()
{
bool result = false;
char[] zip = zipcode.ToString().ToCharArray();
for (int i = 0; i < zip.Length; i++)
{
int val = (int)Char.GetNumericValue(zip[i]);
if ((val.Equals(0)) || (val.Equals(1)) || (val.Equals(2)) || (val.Equals(3)) || (val.Equals(4))|| (val.Equals(5)) || (val.Equals(6))|| (val.Equals(7))|| (val.Equals(8)) || (val.Equals(9)))
{
result = true;
//return result;
}
else
{
result = false;
}
}
return result;
}
private void NextBtn_Click(object sender, RoutedEventArgs e)
{
if (startingIndex < newList.Count)
{
startingIndex++;
AddressTxt.Text = newList[startingIndex].address;
CityTxt.Text = newList[startingIndex].city;
//string str = newList[startingIndex].state;
//for (int i = 0; i < states.Length; i++)
//{
// if (states[i] == str)
// {
// StateCmbo.SelectedIndex = i;
// }
//}
ZipTxt.Text = newList[startingIndex].zip.ToString();
}
else if(startingIndex >= newList.Count)
{
AddressTxt.Text = newList.Last().address;
CityTxt.Text = newList.Last().city;
string str = newList.Last().state;
for (int i = 0; i < states.Length; i++)
{
if (states[i] == str)
{
StateCmbo.SelectedIndex = i + 1;
}
}
ZipTxt.Text = newList.Last().zip.ToString();
}
}
private void PkgsByStateCmbo_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ByStateListBox.Items.Clear();
if (PkgsByStateCmbo.SelectedIndex >= 0)
{
int selectionI = PkgsByStateCmbo.SelectedIndex;
newList = controller.allPackages[selectionI];
foreach (Package p in newList)
{
if (p.state == PkgsByStateCmbo.SelectedItem)
{
ByStateListBox.Items.Add(p.Id.ToString());
}
}
}
}
}
}
我需要MainWindow构造函数和选择更改方法更改的组合。