简体   繁体   English

XAML ListBox仅显示类名称

[英]XAML ListBox only shows class name

How do I get my class properties to show up in the ListBox? 如何获得我的班级属性以显示在列表框中?

XAML: XAML:

<ListBox x:Name="lstPlayers" >
     <DataTemplate>
          <StackPanel Orientation="Horizontal">
              <TextBlock Text="{Binding Player.FirstName}"></TextBlock>
              <TextBlock Text="{Binding Player.LastName}"></TextBlock>
          </StackPanel>
     </DataTemplate>
</ListBox>

C#: C#:

public class Player
{
    string FirstName { get; set; }
    string LastName { get; set; }
}


public void LoadPlayers()
{
    foreach (Player player in Players)
    {
         lstPlayers.Items.Add(player);
     }
}

The only thing that shows up in the ListBox is 列表框中显示的唯一内容是

TestApplication1.Player

You have some problems with you current implementation. 您当前的实现存在一些问题。 First, the DataTemplate should be placed inside the ItemTemplate for the ListBox . 首先,DataTemplate中应放置在里面的ItemTemplate为ListBox Second, the DataContext for each ListBoxItem will be an instance of Player so you should bind directly to FirstName and LastName . 其次,每个ListBoxItem的DataContext将是Player一个实例,因此您应直接绑定到FirstNameLastName Third, the properties in Player should be made public for the DataBinding to work. 第三,应该公开Player的属性以使DataBinding起作用。

<ListBox x:Name="lstPlayers" >
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding FirstName}"></TextBlock>
                <TextBlock Text="{Binding LastName}"></TextBlock>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
public class Player
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

Also, instead of adding the collection item by item to the ListBox , just set it as ItemsSource 另外,不要将收集项逐项添加到ListBox ,而只需将其设置为ItemsSource

lstPlayers.ItemsSource = Players;

DataTemplate应该在ListBox.ItemTemplate内部。

set the collection, Players as ItemSource 设置集合,玩家作为ItemSource

and

<ListBox x:Name="lstPlayers" >
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding FirstName}"></TextBlock>
                    <TextBlock Text="{Binding LastName}"></TextBlock>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

you have to add the DataType to your DataTemplate. 您必须将DataType添加到您的DataTemplate中。

<DataTemplate DataType="{x:Type local:Player}">
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="{Binding FirstName}"></TextBlock>
            <TextBlock Text="{Binding LastName}"></TextBlock>
        </StackPanel>
    </DataTemplate>

local is the namespace for your TestApplication1.Player. local是您的TestApplication1.Player的名称空间。 you can set the datatemplate to the listebox.itemtemplate or as a resource of any "parent object" 您可以将datatemplate设置为listebox.itemtemplate或作为任何“父对象”的资源

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

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