Silverlight Listbox没有绑定

时间:2010-12-17 14:30:22

标签: silverlight data-binding listbox

我试图通过点击按钮来绑定Silverlight列表框。

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

Mainpage.xaml.cs的代码隐藏在这里。我注意到,如果我调用行buildListData();在我设置MyList.DataContext = Listdata之前;然后它工作。否则就像我在这里下面它没有。我必须在按钮单击事件上动态更新列表框然后我通常会调用buildListData();事件处理程序中的方法。这不会在我设置其datacontext后更新 Listdata 属性的值时起作用。有帮助吗?

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

        }
}

这是ListData类

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

    public class ViewModelBase : INotifyPropertyChanged
{



    public event PropertyChangedEventHandler PropertyChanged;

    protected void FirePropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }



}

感谢您的时间......

1 个答案:

答案 0 :(得分:1)

问题是这个(我想)。您绑定到列表。该列表然后得到更新,但是 - 这是有趣的部分 - 列表不明白添加项目意味着它已更新,因此它在技术上不会触发属性更改事件。当你把它作为一个新的List()

时会发生这种情况

所以试试这个 - 首先,转储列表,使用ObservableCollection。等等,我们还没有完成。

现在,为CollectionChanged()事件添加一个处理程序,当你向它添加项目时会触发它。

现在在CollectionChanged事件中,从ListData类中调用FirePropertyChanged(this)。

现在,UI会知道列表已更改,因为项目已添加到其中。

相关问题