【问题标题】:Listbox does not show items c# xaml列表框不显示项目c#xaml
【发布时间】:2015-07-19 15:17:11
【问题描述】:

我已经用 xaml 写了这个:

 <ListBox x:Name="WorkersList">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding name}"></TextBlock>
                        <TextBlock Text="{Binding gehalt}"></TextBlock>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

然后我编写了一个名为“worker”的 c# 类,并将以下代码添加到 mainpage.cs:

    public sealed partial class MainPage : Page
{



    public MainPage()
    {
        this.InitializeComponent();
        List<Worker> sourceworkerlist = new List<Worker>();
        sourceworkerlist.Add(new Worker { name = "Franz", gehalt = 200 });
        WorkersList.DataContext = sourceworkerlist;
    }
}

我运行了程序,但结果是我没有看到 listboxitem :(我做错了什么?谢谢你的回答!

【问题讨论】:

    标签: c# wpf list xaml templates


    【解决方案1】:

    如果您想在发布后的代码中使用DataContext,那么您的 XAML 应该如下所示:

    <ListBox x:Name="WorkersList" d:DataContext="{d:DesignInstance {x:Type local:Worker}}" ItemsSource="{Binding Mode=OneWay}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding name}"/>
                    <TextBlock Text="{Binding gehalt}"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    

    这是完整的 XAML 文件:

    <Window
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        xmlns:local="clr-namespace:WpfApplication11" mc:Ignorable="d" 
        x:Class="WpfApplication11.MainWindow"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <ListBox x:Name="WorkersList" d:DataContext="{d:DesignInstance {x:Type local:Worker}}" ItemsSource="{Binding Mode=OneWay}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{Binding name}"/>
                            <TextBlock Text="{Binding gehalt}"/>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </Grid>
    </Window>
    

    【讨论】:

    • 感谢您的回答,它有效,但我在 xaml 中遇到错误 - 我正在为 Windows 10 通用平台构建:不支持类型...我需要如何更改数据上下文?
    • 我编辑了我的答案并发布了整个 XAML 文件内容。您可能缺少一些 xml 命名空间 (xlmns)。对照我的检查您的 XAML。
    • 它通过保留数据上下文代码行来工作,我已经在 c# 中声明了它 -> 看上面 ;)
    • 我注意到了这一点,但对我来说,它甚至可以将 DataContext 保留在...
    【解决方案2】:

    需要将ItemsSource绑定到DataSource,或者在代码中设置ItemsSource。

    WorkersList.ItemsSource = sourceworkerlist;
    

    <ListBox x:Name="WorkersList" ItemsSource="{Binding}">
    

    【讨论】:

    • @MatthiasHerrmann 不,你没有做我建议的两个解决方案,你没有ItemsSource="{Binding} 也没有WorkersList.ItemsSource = sourceworkerlist;
    • 哦,对不起(这里有超过 35°C ;)我看错了,你说得对
    猜你喜欢
    • 2016-03-08
    • 2012-01-19
    • 1970-01-01
    • 1970-01-01
    • 2010-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多