代码之家  ›  专栏  ›  技术社区  ›  Jonas

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

  •  3
  • Jonas  · 技术社区  · 15 年前

    我正在学习WPF,希望有一个类似LinkedList的集合,在那里我可以添加和删除字符串。我想要一个 ListView 列表视图 在XAML中?

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

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

    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 回复  |  直到 15 年前
        1
  •  4
  •   Alex Telon Jeff Yates    7 年前

    只能将数据绑定到公共属性,并且需要设置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";
        }
    }
    
        2
  •  6
  •   ckozl    12 年前

    DataContext 以编程方式在XAML中设置其他所有内容时,我觉得它不“合适”(也许这只是我自己)。因此,对于下一个人,或者任何其他像我这样思考并从搜索引擎中发现这一点的人(就像我所做的),下面是用XAML实现这一点的方法:

    C级#

    public sealed partial class MainPage : Page
    {
        public ObservableCollection<string> Messages { get; set; }
    
        public MainPage()
        {
            this.Messages = new ObservableCollection<string>();
            this.InitializeComponent();
        }
    }
    

    <Window
        ....
        DataContext="{Binding RelativeSource={RelativeSource Self}}"
        ...>
    
        <ListView ItemsSource="{Binding Messages}" ... />
    
    </Window>
    

    老实说我想 {Binding RelativeSource={RelativeSource Self}} Page Window 等…) DataConext 因为这只是很多人期望的工作方式,我知道这是我假设的工作方式。老实说,我觉得 {Binding RelativeSource={RelativeSource Self}} 有点冗长,对于较短的语法来说几乎很长。