none
一个关于binding的初级问题 RRS feed

  • 问题

  • 我让我的slider绑定到代码里的一个全局变量,并希望该变量一旦变化,则slider自动变化。
    我在点击button的时候改变了该全局变量的值,但是slider毫无反应。

    该变量是一个类的实例,并且实现了Inotifypropertychanged接口,不知道为什么无效?

    代码大致如下:

    全局变量
    public Student st;

     private void Window_Loaded(object sender, RoutedEventArgs e)
            {
                st=new Student(){Length=5};
                Binding b = new Binding("Length");
                b.Source = st;
                b.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
                b.Mode = BindingMode.TwoWay;
                slider1.SetBinding(Slider.ValueProperty, b);

            }

      private void button1_Click(object sender, RoutedEventArgs e)
            {
               st = new Student { Length=7};
              
            }



    类的定义

      public class Student:INotifyPropertyChanged
        {
            public int len;
      
            public int Length
            {

                get
                {
                    return len;
                }
                   
                   
                set
                {
                    len = value;
                    if (PropertyChanged != null)
                    {
                      
                        PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Length"));
                    }
                }
            }

            public event PropertyChangedEventHandler PropertyChanged;

       
        }
    they say nothing last forever
    2009年11月21日 17:57

答案

  •         Student st;
            public Window9()
            {
                InitializeComponent();
                st =new Student{  Length=1};
                Binding b = new Binding("Length");
                b.Source = st;
                b.Mode = BindingMode.TwoWay;
                b.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
                label1.SetBinding(Label.ContentProperty, b);
            }

            private void button1_Click(object sender, RoutedEventArgs e)
            {
                st.Length = 2;

            }
        }
        public class Student : INotifyPropertyChanged
        {
            public int len;

            public int Length
            {

                get
                {
                    return len;
                }


                set
                {
                    len = value;
                    if (PropertyChanged != null)
                    {

                        PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Length"));
                    }
                }
            }

            public event PropertyChangedEventHandler PropertyChanged;


        }

    • 已标记为答案 del.piero 2009年11月22日 15:37
    2009年11月22日 5:25

全部回复

  •         Student st;
            public Window9()
            {
                InitializeComponent();
                st =new Student{  Length=1};
                Binding b = new Binding("Length");
                b.Source = st;
                b.Mode = BindingMode.TwoWay;
                b.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
                label1.SetBinding(Label.ContentProperty, b);
            }

            private void button1_Click(object sender, RoutedEventArgs e)
            {
                st.Length = 2;

            }
        }
        public class Student : INotifyPropertyChanged
        {
            public int len;

            public int Length
            {

                get
                {
                    return len;
                }


                set
                {
                    len = value;
                    if (PropertyChanged != null)
                    {

                        PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Length"));
                    }
                }
            }

            public event PropertyChangedEventHandler PropertyChanged;


        }

    • 已标记为答案 del.piero 2009年11月22日 15:37
    2009年11月22日 5:25
  • 非常感谢,不过附带的有个问题,如果我是listbox绑定到一个List<Student> sts,初始时sts中有一个元素Student的Length=1,然后我在click事件里令sts.First().Length=7;这个时候客户端的listbox仍然没有反应,不知道应该怎么修改?

    2009年11月22日 15:38
  •     private void button1_Click(object sender, RoutedEventArgs e)
            {
                Student w = listBox1.SelectedItem as Student;
                w.Length = 35;
            }
    2009年11月23日 8:03