[英]Bind a ListBox inside an ItemsControl
大家好,我想创建一个客户列表,每个客户都有一组他想购买的商品,就像购物车一样。 我要使用的方法是在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的类。 客户在我的屏幕上显示,但项目没有显示。 所以知道哪里出了问题吗? 那么,知道哪里出了问题吗?
您已经将其与ItemsSource="{Binding Items}"
。 因此,您只需要将Items
属性添加到Cart
类。
public class Cart
{
public ObservableCollection<ShoppingItem> Items { get; private set; }
public Client()
{
Items = new ObservableCollection<ShoppingItem>();
}
}
给定您提交的代码,我确定了错误的出处:
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)
{
Cart cart = new Cart { Client = client };
//Populate the clients list
clients.Add(cart);
using (var file = appStorage.OpenFile(client, System.IO.FileMode.Open))
{
using (var sr = new StreamReader(file))
{
List<string> cartItems = new List<cartItems>();
//Retrieve the content of the file
string fileContent = sr.ReadToEnd();
//Retrieve every item in the file content
for (int i = 0 ; i < fileContent.Length ; i += 20)
{
//Format the file content to the desired format
string savedItems = fileContent.Substring(i, 20);
savedItems = savedItems.Replace("-", " "); // A string is immutable so Replace does not modify savedItems but returns a new string
cartItems.Add(savedItems);
}
cart.Items = cartItems;
}
}
//Populate clientList
clientList.ItemsSource = clients;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.