简体   繁体   中英

Bind list to listboxitem in wpf

I'm trying to bind a list to a listbox in WPF. But it doesn't seem to work, I just see nothing on screen.

Here is my code:

WPF

<ListBox x:Name="listBox" HorizontalAlignment="Left" Height="453" VerticalAlignment="Top" Width="119" Margin="0,43,0,0" ItemsSource="{Binding orderlist}">
        <ListBoxItem Content="{Binding orderlist.ID}"></ListBoxItem>
    </ListBox>

C#

Order order = new Order();
Klantgegevens klantgegevens = new Klantgegevens();
            XmlReader rdr = XmlReader.Create(@"C:\Users\Gebruiker\Desktop\EDI\Rekening.xml");
            rdr.ReadToFollowing("datum");
            order.DatumOntvangst = rdr.ReadElementContentAsString();
            rdr.ReadToFollowing("volgnr");
            order.Status = "Aangenomen";
            order.Opmerkingen = "";
            rdr.ReadToFollowing("naam");
            order.Afzender = rdr.ReadElementContentAsString();
            rdr.ReadToFollowing("naam");
            klantgegevens.Naam = rdr.ReadElementContentAsString();
            rdr.ReadToFollowing("straat");
            klantgegevens.Straat = rdr.ReadElementContentAsString();
            rdr.ReadToFollowing("huisnr");
            klantgegevens.Huisnummer = rdr.ReadElementContentAsInt();
            rdr.ReadToFollowing("plaats");
            klantgegevens.Woonplaats = rdr.ReadElementContentAsString();
            rdr.ReadToFollowing("postcode");
            klantgegevens.Postcode = rdr.ReadElementContentAsString();
            rdr.ReadToFollowing("telefoonnr");
            klantgegevens.Telefoonnummer = rdr.ReadElementContentAsString();
            order.Klantgegevens = klantgegevens;
            orderlist.Add(order);
            listBox.DataContext = orderlist;

As you probably know, Order is a custom class, and so is Klantgegevens. I'm pretty new to binding and WPF in general so excuse me for my stupidness :)

You need to set or bind the ItemsSource property of ListView to an IEnumerable. Since you have set the DataContext property to your "orderlist" you should bind the ItemsSource property directly to the DataContext (ItemsSource="{ Binding }"). You should also use an ItemTemplate as suggested by Fruchtzwerg :

<ListBox x:Name="listBox" HorizontalAlignment="Left" Height="453" VerticalAlignment="Top" Width="119" Margin="0,43,0,0" ItemsSource="{Binding}">
    <ListBox.ItemTemplate>
            <DataTemplate>
                    <TextBlock Text="{Binding ID}" />
            </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

Also note that the DataContext of the ItemTemplate is an item in your ItemsSource, ie an Order object in this case. So to bind to the "ID" property of the Order object you use the binding syntax above. "ID" must be a public property of the Order class.

With

<ListBoxItem Content="{Binding orderlist.ID}"></ListBoxItem>

you are adding an item in XAML. But your plan is to create a template to present bound items. The simplest solution is to use

<ListBox x:Name="listBox" DisplayMemberPath="ID"/>

if only one property needs to be presented. Multiple properties can be showed by creating a template like

<ListView x:Name="listBox">
        <ListView.ItemTemplate>
                <DataTemplate>
                        <StackPanel>
                                <TextBlock Text="{Binding ID}" />
                                <TextBlock Text="{Binding datum}"/>
                                <!-- ... -->
                        </WrapPanel>
                </DataTemplate>
        </ListView.ItemTemplate>
</ListView>

Furthermore you should use a property like

public ObservableCollection<Klantgegevens> Items { get; } =
    new ObservableCollection<Klantgegevens>();

to bind on. Set the DataContext of the whole Window with the ListView to the object, with this property. After that you can bind the ListView with

<ListView ItemsSource="{Binding Items}"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM