【问题标题】:How to show all the data in one ListBox WPF如何在一个 ListBox WPF 中显示所有数据
【发布时间】:2020-12-07 01:43:36
【问题描述】:

我是 WPF 的新手,我有点挣扎,试图在列表中显示所有数据我可以在视图中看到那里的对象(两行),但没有显示像 Name ??有什么推荐吗??谢谢

xaml:

<ListBox x:Name="nadjib">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Path=Name}"/>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

cs:

public ExecuteSpecificJobView()
        {
                InitializeComponent();
                List<string> ext = new List<string>();
                ext.Add(".txt");

                //itemsControl. = new List<string>(){ "ok","yes" };

               ObservableCollection<ViewModels.CreateJobViewModel.Model.TaskJsonAttribute> jobs = new ObservableCollection<ViewModels.CreateJobViewModel.Model.TaskJsonAttribute>();

               jobs.Add(new ViewModels.CreateJobViewModel.Model.TaskJsonAttribute { name = "aliii", source = "ok", target = "yess", type = "complete", extension = ext });
               jobs.Add(new ViewModels.CreateJobViewModel.Model.TaskJsonAttribute { name = "aliii", source = "ok", target = "yess", type = "complete", extension = ext });


               nadjib.ItemsSource = jobs;
        }



      public class TaskJsonAttribute
        {
            public string name;
            public string source;
            public string target;
            public string type;
            public List<string> extension;

            public string Name
            {
                get { return name; }
            }
        }


【问题讨论】:

  • 当您尝试绑定到“名称”(字母大小写)时,您的对象具有属性“名称”。运行程序时,请参阅输出窗口以获取错误消息。
  • 好吧,我的对象还有另一个属性 Name,它返回“name”
  • 你能展示一下TaskJsonAttribute这个类型的样子吗?
  • 当然,TaskJsonAttribute 类已添加!
  • 也许我误解了你的问题。列表框中的两行显示对象名称是可见的。 “显示为名称”是什么意思?它是您希望看到的标题吗?

标签: c# wpf .net-core


【解决方案1】:

这是一个与您的类似的小项目。这在列表框中显示了两个名称。看看这与您的有何不同。

XAML

<Window x:Class="WpfApp9.MainWindow"
        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:WpfApp9"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <ListBox x:Name="nadjib">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Path=Name}"/>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

    </Grid>
</Window>

MainWindow.xaml.cs

using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;

namespace WpfApp9
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            ObservableCollection<TaskJsonAttribute> jobs = new ObservableCollection<TaskJsonAttribute>();

            jobs.Add(new TaskJsonAttribute() { name = "aliii-1" });
            jobs.Add(new TaskJsonAttribute() { name = "aliii-2" });

            nadjib.ItemsSource = jobs;

        }
    }

    public class TaskJsonAttribute
    {
        public string name;
        public string source;
        public string target;
        public string type;
        public List<string> extension;

        public string Name
        {
            get { return name; }
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多