0

大家好,我想创建一个客户列表,每个客户都有一组他想购买的物品,就像购物车一样。我想要这样做的方法是在 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 的类。客户端显示在我的屏幕上,但项目未显示。所以知道问题出在哪里吗?所以知道问题出在哪里吗?

4

1 回答 1

1

您已经将它与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;
    }
}
于 2013-07-22T00:37:27.163 回答