简体   繁体   English

将列表绑定到WPF中的listboxitem

[英]Bind list to listboxitem in wpf

I'm trying to bind a list to a listbox in WPF. 我正在尝试将列表绑定到WPF中的列表框。 But it doesn't seem to work, I just see nothing on screen. 但这似乎不起作用,我在屏幕上什么也看不到。

Here is my code: 这是我的代码:

WPF 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# 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. 您可能知道,Order是一个自定义类,Klantgegevens也是如此。 I'm pretty new to binding and WPF in general so excuse me for my stupidness :) 一般来说,我对绑定和WPF还是很陌生,请原谅我的愚蠢:)

You need to set or bind the ItemsSource property of ListView to an IEnumerable. 您需要将ListView的ItemsSource属性设置或绑定到IEnumerable。 Since you have set the DataContext property to your "orderlist" you should bind the ItemsSource property directly to the DataContext (ItemsSource="{ Binding }"). 由于已将DataContext属性设置为“订单列表”,因此应将ItemsSource属性直接绑定到DataContext(ItemsSource =“ { Binding }”)。 You should also use an ItemTemplate as suggested by Fruchtzwerg : 您还应该使用Fruchtzwerg建议的ItemTemplate:

<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. 还要注意,ItemTemplate的DataContext是ItemsSource中的一个项目,在这种情况下即为Order对象。 So to bind to the "ID" property of the Order object you use the binding syntax above. 因此,要绑定到Order对象的“ ID”属性,请使用上面的绑定语法。 "ID" must be a public property of the Order class. “ ID”必须是Order类的公共属性。

With

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

you are adding an item in XAML. 您正在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. 使用此属性,将具有ListView的整个Window的DataContext设置为对象。 After that you can bind the ListView with 之后,您可以将ListView与

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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