0

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

2 Answers 2

4

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

Additional Info: List vs ObservableCollection vs INotifyPropertyChanged

Sign up to request clarification or add additional context in comments.

Comments

1

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

2 Comments

@Ihor, did the above solution answered your query?
I didn`t try it yet. But thanks for your answer. I will tell you about results

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.