Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# XAML列表框仅显示类名_C#_Wpf_Xaml_Data Binding_Listbox - Fatal编程技术网

C# XAML列表框仅显示类名

C# XAML列表框仅显示类名,c#,wpf,xaml,data-binding,listbox,C#,Wpf,Xaml,Data Binding,Listbox,如何使类属性显示在列表框中 XAML: <ListBox x:Name="lstPlayers" > <DataTemplate> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding Player.FirstName}"></TextBlock> <TextBlock T

如何使类属性显示在列表框中

XAML:

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


public void LoadPlayers()
{
    foreach (Player player in Players)
    {
         lstPlayers.Items.Add(player);
     }
}
列表框中显示的唯一内容是

TestApplication1.Player

DataTemplate应位于ListBox.ItemTemplate内

数据模板应位于ListBox.ItemTemplate内

您当前的实现存在一些问题。首先,DataTemplate应该放在
列表框的ItemTemplate中。其次,每个
ListBoxItem
的DataContext将是
Player
的一个实例,因此您应该直接绑定到
FirstName
LastName
。第三,
Player
中的属性应该公开,以便数据绑定工作

<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>
另外,不要将集合项逐个添加到
列表框
,只需将其设置为ItemsSource即可

lstPlayers.ItemsSource = Players;

您当前的实现存在一些问题。首先,DataTemplate应该放在
列表框的ItemTemplate中。其次,每个
ListBoxItem
的DataContext将是
Player
的一个实例,因此您应该直接绑定到
FirstName
LastName
。第三,
Player
中的属性应该公开,以便数据绑定工作

<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>
另外,不要将集合项逐个添加到
列表框
,只需将其设置为ItemsSource即可

lstPlayers.ItemsSource = Players;

将集合、玩家设置为ItemSource


将收藏、玩家设置为ItemSource


您必须将数据类型添加到数据模板中

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


local是TestApplication1.Player的命名空间。您可以将datatemplate设置为ListBox.itemtemplate,或者将其作为任何“父对象”的资源,您必须将数据类型添加到datatemplate中

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


local是TestApplication1.Player的命名空间。您可以将datatemplate设置为ListBox.itemtemplate或作为任何“父对象”的资源。第二点,我看不到,因为在iPhone上StackOverflow没有显示代码部分的滚动,我只能看到“{Binding”…@Akash Kava:我在iPhone上也遇到了同样的问题,但我最近注意到,如果你用两个手指点击/按下代码,你就无法滚动。试试看:)非常感谢,它很有效,可惜到目前为止还没有人告诉我!!+1是第二点,我看不见,因为iPhone上StackOverflow没有显示代码部分的滚动,我只能请参阅“{Binding”…@Akash-Kava:我在iPhone上也遇到了同样的问题,但我最近注意到,如果你用两个手指点击/按下代码,你就无法滚动代码。试试看:)非常感谢,它很有效,可惜到目前为止没有人告诉我!!