WPF 界面與后台雙向數據綁定


    <Label x:Name="lbScore1" Content="{Binding Score1, Mode=TwoWay}" Width="200" FontSize="15" VerticalContentAlignment="Center" HorizontalContentAlignment="Center"/>
            <Label x:Name="lbScore2" Content="{Binding Score2, Mode=TwoWay}"  Width="200" FontSize="15" VerticalContentAlignment="Center" HorizontalContentAlignment="Center"/>
            <Label x:Name="lbScore3" Content="{Binding Score3, Mode=TwoWay}"  Width="200" FontSize="15" VerticalContentAlignment="Center" HorizontalContentAlignment="Center"/>
            <Label x:Name="lbScore4" Content="{Binding Score4, Mode=TwoWay}"  Width="200" FontSize="15" VerticalContentAlignment="Center" HorizontalContentAlignment="Center"/>
            <Label x:Name="lbScore5" Content="{Binding Score5, Mode=TwoWay}"  Width="200" FontSize="15" VerticalContentAlignment="Center" HorizontalContentAlignment="Center"/>

1.UI界面里面的Label綁定數據,雙向綁定用TwoWay,默認為OneWay,這兒必須加上Mode=TwoWay。

 

  public class UserScore : INotifyPropertyChanged
    {
        private string userName;
        private string score1;
        private string score2;
        private string score3;
        private string score4;
        private string score5;

        public string UserName
        {
            get
            {
                return userName;
            }
            set
            {
                this.userName = value;
            }
        }

        public string Score1
        {
            get
            {
                return score1;
            }
            set
            {
                this.score1 = value;//value轉到定義,?其實就是public int age//這里暫時不知道這種機制

                if (PropertyChanged != null)//如果沒有點擊實現接口,就沒有propertychanged這個成員
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(Score1));
                }
            }
        }
        public string Score2
        {
            get
            {
                return score2;
            }
            set
            {
                this.score2 = value;

                if (PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(Score2));
                }
            }
        }
        public string Score3
        {
            get
            {
                return score3;
            }
            set
            {
                this.score3 = value;

                if (PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(Score3));
                }
            }
        }
        public string Score4
        {
            get
            {
                return score4;
            }
            set
            {
                this.score4 = value;

                if (PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(Score4));
                }
            }
        }
        public string Score5
        {
            get
            {
                return score5;
            }
            set
            {
                this.score5 = value;

                if (PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(Score5));
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }

2.后台類代碼:必須為INotifyPropertyChanged接口類, public class UserScore : INotifyPropertyChanged //在此要選擇INotifyPropertyChanged,右鍵,解析得到命名空間,但還要增多一步:選擇INotifyPropertyChanged,右鍵,實現接口。

 

     private void BindingData()
        {
            for (int i = 0; i < 10; i++)
            {
                ctlList[i].lbScore1.DataContext = scoreList[curPage * 10 + i];
                ctlList[i].lbScore2.DataContext = scoreList[curPage * 10 + i];
                ctlList[i].lbScore3.DataContext = scoreList[curPage * 10 + i];
                ctlList[i].lbScore4.DataContext = scoreList[curPage * 10 + i];
                ctlList[i].lbScore5.DataContext = scoreList[curPage * 10 + i];
            }
        }

3.cs后台添加數據綁定對象,scoreList鏈表里面存放的是UserScore接口類,ctlList存放的是控件。這樣就實現了雙向數據綁定,任何一方改變都會有更新,在通過數據庫數據對接,就基本上實現了數據的雙向通信。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM