简体   繁体   English

数据未绑定到ListBox WPF

[英]Data not binding to ListBox WPF

XAML XAML

<Page x:Class="ManufacturingWPF.ShowHardware"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  xmlns:local="clr-namespace:ManufacturingWPF"
  mc:Ignorable="d" 
  d:DesignHeight="300" d:DesignWidth="300"
  Title="ShowHardware">



<Grid Background="AliceBlue">
    <ListBox x:Name="HardwareList" ItemsSource="{Binding Hardware}"  HorizontalAlignment="Left" Height="122" Margin="76,36,0,0" VerticalAlignment="Top" Width="149">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <TextBlock Text="{Binding ID}"/>
                    <TextBlock Text="{Binding Date}"/>
                    <TextBlock Text="{Binding Nodes}"/>
                    <TextBlock Text="{Binding Repeaters}"/>
                    <TextBlock Text="{Binding Hubs}"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

</Grid>

Code Behind C# C#背后的代码

public partial class ShowHardware : Page
{
    public ShowHardware()
    {
        InitializeComponent();
        DisplayData();

    }

    public void DisplayData()
    {
         //Datamodel MDM used for ADO and table creation
         //Test is a class used to pass the model and as the name suggest 
         test it

        ManufacturingDataModel MDM = new ManufacturingDataModel();
        Test t = new Test(MDM);

        List<Hardware> x = t.GetHardware();

        foreach(Hardware i in x )
        {
            HardwareList.ItemsSource = i.Hubs.ToString();
        }
    }

}

} }

I'm facing issues binding the data to the listbox as shown in the XAML and code-behind content. 我面临将数据绑定到列表框的问题,如XAML和代码隐藏内容所示。 I've tried previous answers without any luck , did my research but apparently I'm missing out something or maybe there is something I don't quite understand. 我没有运气就尝试了以前的答案,做了我的研究,但显然我错过了一些东西,或者也许有些我不太了解的东西。 Itemsource as the name suggest should bind to the source of where my data is being held. 顾名思义,Itemsource应该绑定到保存我的数据的源。 In this case the source would be my class Hardware that holds data for Nodes, date , hubs etc etc. And in the textblock I manually bind these properties and display the values. 在这种情况下,源将是我的类Hardware,其中包含Node,date,hub等的数据。在文本块中,我手动绑定这些属性并显示值。 But this is not working. 但这是行不通的。 PS My DB table is populated. PS填充了我的数据库表。

This code seems wrong 该代码似乎是错误的

foreach(Hardware i in x )
{
    HardwareList.ItemsSource = i.Hubs.ToString();
}

Then ItemsSource for Binding has to be a Collection ( List , ObservableCollecion , IEnumerable<> ...). 然后用于绑定的ItemsSource必须是一个Collection( ListObservableCollecionIEnumerable<> ...)。

Try HardwareList.ItemsSource = x; 尝试HardwareList.ItemsSource = x; and remove the foreach loop 并删除foreach loop

I hope this can help you. 希望对您有所帮助。

That's because the ItemsSource is an IEnumerable and you assign to it the list of hardware itself. 这是因为ItemsSource是IEnumerable,并且您为其分配了硬件本身的列表。 So you code should look something like this : 因此,您的代码应如下所示:

ManufacturingDataModel MDM = new ManufacturingDataModel();
Test t = new Test(MDM);

 List<Hardware> x = t.GetHardware();
 HardwareList.ItemsSource = x;

//or
foreach (Hardware h in x)
  HardwareList.Items.Add(h);

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

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