Reputation: 1330
Hi I want to fill a list box with the items of an observable collection. I have in my XAML file:
<catel:UserControl x:Class="Musat.Classificator.CatelMVVM.Views.StopControlView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:catel="http://catel.codeplex.com"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<catel:UserControl.Resources>
<CollectionViewSource Source="{Binding Something}" x:Key="cvsStops" />
</catel:UserControl.Resources>
<catel:StackGrid x:Name="LayoutRoot">
<ListBox ItemsSource="{Binding Source={StaticResource cvsStops}}" />
</catel:StackGrid>
and in my ViewModel I have:
class StopViewModel : ViewModelBase
{
public StopViewModel()
{
ObservableCollection<String> Something = new ObservableCollection<String>();
Something.Add("A");
Something.Add("B");
Something.Add("C");
Something.Add("D");
Something.Add("E");
Something.Add("F");
}
}
However the list box is not filled with any data. Is there something that I am doing wrong because I can't find it?
Upvotes: 1
Views: 1189
Reputation: 2902
We are sort of guessing here, but could it be as simple as this? You must bind to either a property(with INotifyPropertyChanged) or a Dependency Property in a DO.
private ObservableCollection<string> something = new ObservableCollection<String> { "A", "B", "C", "D", "E", "F" };
public ObservableCollection<String> Something // Must be property or DP to be bound!
{
get { return something; }
set
{
if (Equals(value, something)) return;
something = value;
RaisePropertyChanged("Something");
}
}
Hard to do tell without your full code. How have you set your datacontext? Why are you not binding directly to this property?
<ListBox ItemsSource="{Binding Something}"/>
Check your debug output when starting the application.
Cheers
Stian
Upvotes: 3