Ihor Korotenko
Ihor Korotenko

Reputation: 906

Binding List<string> property to Listbox WPF

Can someone help me? I have the folowing XAML code in my MainWindow.xaml file:

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

And in my ViewModel.cs I have property:

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

But, when I click the button and add some items to Files nothing happens.

P.S. Sorry for my bad English :)

Upvotes: 0

Views: 4032

Answers (2)

Dinesh Kumar P
Dinesh Kumar P

Reputation: 1178

Here is your solution, just add this code and press 'Add String' button to make it work. I have used 'ObservableCollection' instead of List and made to listen it using 'INotifyPropertyChanged' interface in ViewModel.cs class

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

Upvotes: 1

Michal Ciechan
Michal Ciechan

Reputation: 13888

List does not implement INotifyCollectionChanged, instead of List, use ObservableCollection<string>

Additional Info: List vs ObservableCollection vs INotifyPropertyChanged

Upvotes: 4

Related Questions