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

3

我正在学习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# 代码:

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个回答

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}}有点啰嗦,对于更短的语法来说几乎有点长了。


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";
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接