WPF,列表框中没有显示任何内容

发布于 2024-09-14 13:10:08 字数 1257 浏览 4 评论 0原文

我不知道我在这里做错了什么。我有一个 ListBox,其 DataContextItemsSource 已设置,但是当我运行我的程序时,ListBox 中没有任何内容应用程序。调试时,获取 ListBox 项目的方法的第一行永远不会被命中。这是我所拥有的:

// Constructor in UserControl
public TemplateList()
{
    _templates = new Templates();
    InitializeComponent();
    DataContext = this;
}

// ItemsSource of ListBox
public List<Template> GetTemplates()
{
    if (!tryReadTemplatesIfNecessary(ref _templates))
    {
        return new List<Template>
            {
                // Template with Name property set:
                new Template("No saved templates", null)
            };
    }
    return _templates.ToList();
}

这是我的 XAML:

<ListBox ItemsSource="{Binding Path=GetTemplates}" Grid.Row="1" Grid.Column="1"
         Width="400" Height="300" DisplayMemberPath="Name"
         SelectedValuePath="Name"/>

Template 类的实例上,有一个 Name 属性,它只是一个 string。我想要的只是显示模板名称列表。用户不会更改 Template 中的任何数据,ListBox 只需要只读即可。

模板还有一个 Data 属性,稍后我将在这个 ListBox 中显示该属性,因此我不想让 GetTemplates 仅返回一个列表字符串——它需要返回一些 Template 对象的集合。

I don't know what I'm doing wrong here. I have a ListBox whose DataContext and ItemsSource are set, but there is nothing in the ListBox when I run my app. When debugging, the first line of my method for getting items for the ListBox never gets hit. Here's what I have:

// Constructor in UserControl
public TemplateList()
{
    _templates = new Templates();
    InitializeComponent();
    DataContext = this;
}

// ItemsSource of ListBox
public List<Template> GetTemplates()
{
    if (!tryReadTemplatesIfNecessary(ref _templates))
    {
        return new List<Template>
            {
                // Template with Name property set:
                new Template("No saved templates", null)
            };
    }
    return _templates.ToList();
}

Here's my XAML:

<ListBox ItemsSource="{Binding Path=GetTemplates}" Grid.Row="1" Grid.Column="1"
         Width="400" Height="300" DisplayMemberPath="Name"
         SelectedValuePath="Name"/>

On an instance of the Template class, there's a Name property that is just a string. All I want is to display a list of template names. The user won't change any data in a Template, the ListBox just needs to be read-only.

A Template also has a Data property that I will later display in this ListBox, so I don't want to make GetTemplates return just a list of strings--it needs to return some collection of Template objects.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

素衣风尘叹 2024-09-21 13:10:08

您无法绑定到方法。让它成为一个属性,它应该可以工作。

最好将 List 设置为 DataContext,或者创建一个保存列表的 ViewModel。这样,您将可以更好地控制列表框将绑定到的实例。

希望这有帮助!

You can't bind to a method. Make it a property and it should work.

Its better though to set the List as DataContext, or create a ViewModel that holds the list. Thay way, you will have more control over the instances your Listbox will bind to.

Hope this helps!

倾`听者〃 2024-09-21 13:10:08

当您应该使用属性时,您正在尝试调用绑定中的方法。将其更改为属性,然后您就可以开始了。

public List<Template> MyTemplates {get; private set;}

public TemplateList()
{
    InitializeComponent();
    SetTemplates();
    DataContext = this;
}

// ItemsSource of ListBox
public void SetTemplates()
{
    // do stuff to set up the MyTemplates proeprty
    MyTemplates = something.ToList();
}

XML:

<ListBox ItemsSource="{Binding Path=MyTemplates}" Grid.Row="1" Grid.Column="1"
   Width="400" Height="300" DisplayMemberPath="Name"
   SelectedValuePath="Name"/>

You're attempting to call a method in your binding when you should be using a property. Change it to a property and you should be good to go.

public List<Template> MyTemplates {get; private set;}

public TemplateList()
{
    InitializeComponent();
    SetTemplates();
    DataContext = this;
}

// ItemsSource of ListBox
public void SetTemplates()
{
    // do stuff to set up the MyTemplates proeprty
    MyTemplates = something.ToList();
}

Xaml:

<ListBox ItemsSource="{Binding Path=MyTemplates}" Grid.Row="1" Grid.Column="1"
   Width="400" Height="300" DisplayMemberPath="Name"
   SelectedValuePath="Name"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文