antanas_sepikas
antanas_sepikas

Reputation: 5704

C# wpf observablecollection binding to xaml

I am trying to use ObservableCollection to bind data from object to xaml view: I have a class which has property :

public ObservableCollection<Roll> RollList = new ObservableCollection<Roll>();

And some methods that modify that collection (basically method that adds new entries), like this:

RollList.Add(roll); //roll is and Roll class object bellow

Here's a roll class that I use in collection:

class Roll : INotifyPropertyChanged
{
    private List<int> _hitList;

    public List<int> HitList
    {
        get { return _hitList; }
        set { _hitList = value; OnPropertyChanged("HitList"); }
    }

    public event PropertyChangedEventHandler PropertyChanged;

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

public class ListToStringConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        List<int> list = value as List<int>;
        return String.Join("", list.ToArray());
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        string list = value as string;
        return list.Split(' ').Select(n => int.Parse(n)).ToList();
    }
}

Now in my main window class I instantiate my class which constructs ObservableCollection object above and fills it with data;

I bind my collection to DataContex like so;

DataContext = MyCoolClass; //MyCoolClass has ObservableCollection<Roll> RollList inside of it

And last thing I what I do:

<Window.Resources>
    <local:ListToStringConverter x:Key="ListToStringConverter" />
</Window.Resources>

<ListBox 
    Height="Auto" 
    Width="Auto" 
    Name="RollList" 
    ItemsSource="{Binding RollList, Converter={StaticResource ListToStringConverter}}"
/>

In the the listbox doesn't get populated with data. I know that RollList object is filled with data, because I can is it in watch window, and if I assign listbox item source manualy:

RollList.ItemsSource = ConvertedCollection;

It works and listbox is populated with data I wan't, but I want to bind it inside xaml;

PS. I am new to C# and WPF.

Upvotes: 0

Views: 1666

Answers (2)

Derrick Moeller
Derrick Moeller

Reputation: 4950

You'll need to implement a property.

public MyCoolClass
{
    private ObservableCollection<Roll> _rollList;

    public ObservableCollection<Roll> RollList
    {
        get { return _rollList; }
        set
        {
            if (_rollList != value)
            {
                _rollList = value;
                OnPropertyChanged("RollList");
            }
        }
    }
}

Upvotes: 0

Erti-Chris Eelmaa
Erti-Chris Eelmaa

Reputation: 26268

public ObservableCollection<Roll> RollList = new ObservableCollection<Roll>();

That's not a property. That's a field. WPF works with properties.

Upvotes: 7

Related Questions