大家好,我想创建一个客户列表,每个客户都有一组他想购买的物品,就像购物车一样。我想要这样做的方法是在 ItemsControl 中创建一个 ListBox,如下所示:
<ItemsControl Name="clientList">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid Height="46">
<Grid.RowDefinitions>
<RowDefinition Height="46"/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Name="client" Text="{Binding Client}" Height="45" Width="200" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,5" FontSize="26"/>
<ListBox Name="itemsList" Grid.Row="1" ItemsSource="{Binding Items}"/>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
我确实知道如何填充 clientList,但 itemsList 我只是不知道如何绑定它我的代码中有 List,但是我将如何从后面的代码访问 itemsList 以将其与集合绑定。
这是我背后的代码:
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
//Load the files in the Isolated Storage
var appStorage = IsolatedStorageFile.GetUserStoreForApplication();
//Retrieve the files
string[] clientlist = appStorage.GetFileNames();
foreach (string client in clientlist)
{
//Populate the clients list
clients.Add(new Cart { Client = client });
using (var file = appStorage.OpenFile(client, System.IO.FileMode.Open))
{
using (var sr = new StreamReader(file))
{
//Retrieve the content of the file
string fileContent = sr.ReadToEnd();
//Retrieve every item in the file content
int i = 0;
while (i < fileContent.Length)
{
//Format the file content to the desired format
string savedItems = fileContent.Substring(i, 20);
savedItems.Replace("-", " ");
//Populate the items ListBox
items.Add(new Cart { Items = savedItems });
//Move to the next item
i += 20;
}
//Populate clientList
clientList.ItemsSource = clients;
}
}
}
}
Cart 是一个具有两个字符串属性 Client 和 Items 的类。客户端显示在我的屏幕上,但项目未显示。所以知道问题出在哪里吗?所以知道问题出在哪里吗?