如何将List作为ItemSource绑定到XAML中的ListView?

时间:2010-06-14 18:01:42

标签: wpf data-binding xaml listview collections

我正在学习WPF,并希望有一个类似于LinkedList的集合,我可以添加和删除字符串。我希望有一个ListView用数据绑定来收听该集合。如何将简单列表集合绑定到XAML中的ListView

我的想法(不工作)是这样的:

<Window x:Class="TestApp.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <LinkedList x:Key="myList"></LinkedList> //Wrong
    <Window.Resources>
    <Grid>
    <ListView Height="100" HorizontalAlignment="Left" Margin="88,134,0,0" 
      Name="listView1" VerticalAlignment="Top" Width="120" 
      ItemsSource="{Binding Source={StaticResource myList}}"/> //Wrong
    </Grid>
</Window>

我的所有代码(更新版本,无效):

<Window x:Class="TestApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TextBox Height="23" HorizontalAlignment="Left" Margin="12,12,0,0" 
          Name="textBox1" VerticalAlignment="Top" Width="120" />
        <Button Content="Button" Height="23" HorizontalAlignment="Right" 
          Margin="0,12,290,0" Name="button1" VerticalAlignment="Top" Width="75" 
          Click="button1_Click" />
        <ListView Height="100" HorizontalAlignment="Left" Margin="88,134,0,0" 
          Name="listView1" VerticalAlignment="Top" Width="120" 
          ItemsSource="{Binding myList}"/>
    </Grid>
</Window>

C#-code:

namespace TestApp
{
    public partial class MainWindow : Window
    {
        ObservableCollection<string> myList = new ObservableCollection<string>();
        public MainWindow()
        {
            InitializeComponent();
            myList.Add("first string");
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            myList.Add(textBox1.Text);
            textBox1.Text = myList.Count+"st";
        }
    }
}

2 个答案:

答案 0 :(得分:6)

选择作为答案的方法很好......但我并不特别喜欢在XAML中设置其他所有内容时必须以编程方式指定DataContext,我不觉得它是“正确的”(也许这只是我)。所以对于下一个人,或者任何像我一样认为并从搜索引擎中发现这一点的人(就像我一样),这就是在XAML中实现它的方法:

<强> C#

public sealed partial class MainPage : Page
{
    public ObservableCollection<string> Messages { get; set; }

    public MainPage()
    {
        this.Messages = new ObservableCollection<string>();
        this.InitializeComponent();
    }
}

<强> XAML

<Window
    ....
    DataContext="{Binding RelativeSource={RelativeSource Self}}"
    ...>

    <ListView ItemsSource="{Binding Messages}" ... />

</Window>

说实话,我认为{Binding RelativeSource={RelativeSource Self}}应该是任何顶级元素的默认值(PageWindow等等......)DataConext,因为它只是如何很多人都希望它能起作用,我知道这是我认为它会起作用的方式。老实说,我觉得{Binding RelativeSource={RelativeSource Self}}有点冗长,而且语法更短。

答案 1 :(得分:4)

您只能数据绑定到公共属性,并且需要设置DataContext。

public partial class MainWindow : Window
{
    public ObservableCollection<string> myList { get; private set; }

    public MainWindow()
    {
        InitializeComponent();

        myList = new ObservableCollection<string>();
        myList.Add("first string");
        DataContext = this;
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        myList.Add(textBox1.Text);
        textBox1.Text = myList.Count + "st";
    }
}