将List <string>属性绑定到Listbox WPF </string>

时间:2014-12-01 23:25:06

标签: wpf

有人能帮助我吗?我在MainWindow.xaml文件中有以下XAML代码:

       <ListBox ItemsSource="{Binding Files}" HorizontalAlignment="Left" 
                Height="371"   Margin="281,53,0,0" VerticalAlignment="Top" 
                Width="609">
             <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding}" />
                    </StackPanel>

                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

在我的ViewModel.cs中,我有属性:

public List<string> Files { get; set; }

但是,当我点击按钮并向文件中添加一些项目时,没有任何事情发生。

P.S。抱歉我的英语不好:)

2 个答案:

答案 0 :(得分:3)

List未实施INotifyCollectionChanged,而不是列表,请使用ObservableCollection<string>

其他信息:List vs ObservableCollection vs INotifyPropertyChanged

答案 1 :(得分:1)

以下是您的解决方案,只需添加此代码并按“添加字符串”即可。按钮使其工作。我使用过&#39; ObservableCollection&#39;而不是列表,并使用&#39; INotifyPropertyChanged&#39;听取它ViewModel.cs类中的接口

MainWindow.xaml

    <Window x:Class="ListBox_Strings.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:myApp="clr-namespace:ListBox_Strings"
        Title="MainWindow" Height="350" Width="525">
    <Window.DataContext>
        <myApp:ViewModel/>
    </Window.DataContext>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition Height="50"/>
        </Grid.RowDefinitions>
        <ListBox ItemsSource="{Binding Files}" HorizontalAlignment="Left" 
                 VerticalAlignment="Top" >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding}" />
                    </StackPanel>

                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <Button Grid.Row="1" Content="Add String" Click="Button_Click"></Button>
    </Grid>
</Window>

MainWindow.xaml.cs

using System.Windows;
namespace ListBox_Strings
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var vm = this.DataContext as ViewModel;
            vm.Files.Add("New String");
        }
    }
}

ViewModel.cs

using System.Collections.ObjectModel;
using System.ComponentModel;

namespace ListBox_Strings
{
    public class ViewModel:INotifyPropertyChanged
    {
        private ObservableCollection<string> m_Files;

        public ObservableCollection<string> Files
        {
            get { return m_Files; }
            set { m_Files = value; 
                OnPropertyChanged("Files"); }
        }


        public ViewModel()
        {
            Files = new ObservableCollection<string>();
            Files.Add("A");
            Files.Add("B");
            Files.Add("C");
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(string name)
        {
            if (PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(name));
            }
        }
    }
}