如何字符串列表我的数据绑定到WPF / WP7一个ListBox?我的、字符串、绑定、数据

2023-09-02 21:24:56 作者:劝君莫执意

我想字符串值列表绑定到一个列表框,使它们的值列一行一行。现在,我用这样的:

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>

但我不知道我应该代替编号装进文本块,,因为它们是所有的字符串值,而不是自定义类。

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.

此外,报告说没有找到PersonNames时,我有它MainPage的里面,因为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的时候。继code是你会被传递到页面的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:

And the 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>

希望这有助于! :)

Hope this helps! :)