在ItemsControl中绑定ListBox

时间:2013-07-22 00:10:37

标签: c# wpf silverlight xaml

好日子,伙计们, 我想创建一个客户列表,每个客户都有一组他想要购买的商品,就像购物车一样。 我想要使​​用的方法是在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的类。客户端显示在我的屏幕上,但项目未显示。所以任何想法问题在哪里? 所以任何想法问题在哪里?

1 个答案:

答案 0 :(得分: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;
    }
}