Silverlight Listbox not binding
I am trying to bind a Silverlight listbox thru the codebehind on a button click.
Here is the XAML
<ListBox x:Name="MyList" DataContext="Listdata" ItemsSource="{Binding Mode=TwoWay, Path=Listdata}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name,Mode=TwoWay}" Margin="5" Foreground="Red"></TextBlock>
<TextBlock Text="{Binding Age,Mode=TwoWay}" Margin="5"></TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Codehind of Mainpage.xaml.cs here. I've noticed that if I call the line buildListData(); before I set MyList.DataContext = Listdata; then it works. Otherwise like I have here below it doesnt. Suppse I have to update the Listbox dynamically on a button click event then I would typically call the buildListData(); method in its event handler. This would not work either as Im updating the value of the Listdata property after setting its datacontext. Any help?
public partial class MainPage : UserControl, INotifyPropertyChanged
{
private List<ListData> _listData;
public List<ListData> Listdata
{
get {return _listData ;开发者_开发问答}
set { _listData = value; FirePropertyChanged("Listdata"); }
}
public event PropertyChangedEventHandler PropertyChanged;
protected void FirePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public MainPage()
{
// Required to initialize variables
InitializeComponent();
MyList.DataContext = Listdata;
MyList.ItemsSource = Listdata;
buildListData();
FirePropertyChanged("Listdata");
}
private void buildListData ()
{
Listdata = new List<ListData>();
Listdata.Add(new ListData(){Name="Person 1", Age=30});
Listdata.Add(new ListData() { Name = "Person 2", Age = 31 });
Listdata.Add(new ListData() { Name = "Person 3", Age = 32 });
Listdata.Add(new ListData() { Name = "Person 4", Age = 33 });
Listdata.Add(new ListData() { Name = "Person 5", Age = 34 });
}
}
Here is the ListData class
public class ListData : ViewModelBase
{
private string _name;
private int _age;
public string Name
{
get { return _name; }
set { _name = value; FirePropertyChanged("Name"); }
}
public int Age
{
get { return _age; }
set { _age = value; FirePropertyChanged("Age"); }
}
}
ViewMOdelBase class here
public class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void FirePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
Thanks for your time...
The problem is this (I think). You are binding to a list. The list then gets updated, BUT - here's the funny part - the list doesn't understand that adding Items to it means it was updated, so it doesn't technically fire the property changed event. That happens when you make it a new List()
So try this - first, dump the list, use an ObservableCollection. Wait, we aren't done.
Now, add a handler for the CollectionChanged() event, which will fire when you add items to it.
Now in the CollectionChanged event, call FirePropertyChanged(this) from the ListData class.
Now the UI will know the list changed because items were added to it.
精彩评论