如何将字符串列表数据绑定到 WPF/WP7 中的 ListBox? [英] How can I data bind a list of strings to a ListBox in WPF/WP7?

查看:43
本文介绍了如何将字符串列表数据绑定到 WPF/WP7 中的 ListBox?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将字符串值列表绑定到列表框,以便逐行列出它们的值.现在我用这个:

I am trying to bind a list of string values to a listbox so that their values are listed line by line. Right now I use this:

<ListBox Margin="20" ItemsSource="{Binding Path=PersonNames}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Path=Id}"></TextBlock>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

但我不知道我应该在文本块中放入什么,而不是 Id,因为它们都是字符串值,而不是自定义类.

But I don't know what I am supposed to put into the textblock, instead of Id, since they are all string values, not custom classes.

当我在 MainPage 中拥有 PersonNames 时,它也抱怨不必找到 PersonNames,如 MainPage.PersonNames.

Also it complains not having to find the PersonNames when I have it inside MainPage, as MainPage.PersonNames.

我将数据上下文设置为:

I set the data context to:

DataContext="{Binding RelativeSource={RelativeSource Self}}"

我做错了吗?

推荐答案

如果简单地说你的 ItemsSource 是这样绑定的:

If simply put that your ItemsSource is bound like this:

YourListBox.ItemsSource = new List<String> { "One", "Two", "Three" };

您的 XAML 应如下所示:

Your XAML should look like:

<ListBox Margin="20" Name="YourListBox">
    <ListBox.ItemTemplate> 
        <DataTemplate> 
            <StackPanel Orientation="Horizontal"> 
                <TextBlock Text="{Binding}" /> 
            </StackPanel> 
        </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

更新:

这是使用 DataContext 时的解决方案.以下代码是您将传递给页面的 DataContext 的视图模型以及 DataContext 的设置:

This is a solution when using a DataContext. Following code is the viewmodel you will be passing to the DataContext of the page and the setting of the DataContext:

public class MyViewModel
{
    public List<String> Items
    {
        get { return new List<String> { "One", "Two", "Three" }; }
    }
}

//This can be done in the Loaded event of the page:
DataContext = new MyViewModel();

您的 XAML 现在看起来像这样:

Your XAML now looks like this:

<ListBox Margin="20" ItemsSource="{Binding Items}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding}" />
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

这种方法的优点是您可以在 MyViewModel 类中放置更多属性或复杂对象,并在 XAML 中提取它们.例如传递 Person 对象列表:

The advantage of this approach is that you can put a lot more properties or complex objects in the MyViewModel class and extract them in the XAML. For example to pass a List of Person objects:

public class ViewModel
{
    public List<Person> Items
    {
        get
        {
            return new List<Person>
            {
                new Person { Name = "P1", Age = 1 },
                new Person { Name = "P2", Age = 2 }
            };
        }
    }
}

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

还有 XAML:

<ListBox Margin="20" ItemsSource="{Binding Items}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Path=Name}" />
                <TextBlock Text="{Binding Path=Age}" />
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

希望这有帮助!:)

这篇关于如何将字符串列表数据绑定到 WPF/WP7 中的 ListBox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆