WPF 數據綁定,界面刷新的兩種方法-----INotifyPropertyChanged


.Netformwork4.0及以下版本 -------INotifyPropertyChanged

命名空間: System.ComponentModel

后台代碼

public partial class DvrWnd : UserControl
    {
        public DvrWnd()
        {
            InitializeComponent();
        }

        private void InitInfo()
        {
            for (int i = 0; i < 10; i++)
            {
                DvrInfo dvrInfo = new DvrInfo();
                dvrInfo.strDvrName = "編碼器" + i.ToString();
                dvrInfo.strDvrIp = "10.10.13.10" + i.ToString();
                dvrInfo.strDvrPort = "506" + i.ToString();
                dvrInfo.strDvrType = "Type " + i.ToString();
                dvrInfo.strDvrCode = "340201030002150000" + i.ToString();
                dvrInfoList.Items.Add(dvrInfo);
            }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            dvrInfoList.Items.Clear();
            InitInfo();
        }

        //編碼器信息, 只要改了類中的屬性, 就能隨便用
        public class DvrInfo : INotifyPropertyChanged
        {
            private string m_strDvrIp = string.Empty;
            private string m_strDvrPort = string.Empty;
            private string m_strDvrName = string.Empty;
            private string m_strDvrType = string.Empty;
            private string m_strDvrCode = string.Empty;
            public string strDvrIp
            {
                set
                {
                    if (m_strDvrIp != value)
                    {
                        m_strDvrIp = value;
                    }
                    OnPropertyChanged("strDvrIp");
                }
                get
                {
                    return m_strDvrIp;
                }
            }
            public string strDvrPort
            {
                set
                {
                    if (m_strDvrPort != value)
                    {
                        m_strDvrPort = value;
                        OnPropertyChanged("strDvrPort");
                    }
                }
                get
                {
                    return m_strDvrPort;
                }
            }
            public string strDvrName
            {
                set
                {
                    if (m_strDvrName != value)
                    {
                        m_strDvrName = value;
                        OnPropertyChanged("strDvrName");
                    }
                }
                get
                {
                    return m_strDvrName;
                }

            }
            public string strDvrType
            {
                set
                {
                    if (m_strDvrType != value)
                    {
                        m_strDvrType = value;
                        OnPropertyChanged("strDvrType");
                    }
                }
                get
                {
                    return m_strDvrType;
                }

            }
            public string strDvrCode
            {
                set
                {
                    if (m_strDvrCode != value)
                    {
                        m_strDvrCode = value;
                        OnPropertyChanged("strDvrCode");
                    }
                }
                get
                {
                    return m_strDvrCode;
                }
            }

            public event PropertyChangedEventHandler PropertyChanged;
            private void OnPropertyChanged(string strPropertyInfo)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(strPropertyInfo));
                }
            }
        }

  

  前端代碼綁定

<UserControl x:Class="testDevice.DvrWnd"
             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" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300"
             Background="White"
             Loaded="UserControl_Loaded"
             >
    <Grid>
        <DataGrid Name="dvrInfoList" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTextColumn Header="編碼器名" IsReadOnly="True" Width="2*" Binding ="{Binding strDvrName}" /> 
                  //紅色標記部分為OnPropertyChanged("strDvrName");中strDvrName, 並不是DvrInfo.strDvrName中的strDvrName <DataGridTextColumn Header="編碼器IP" Width="*" IsReadOnly="True" Binding="{Binding strDvrIp}"/> <DataGridTextColumn Header="編碼器Port" Width="*" IsReadOnly="True" Binding="{Binding strDvrPort}"/> <DataGridTextColumn Header="編碼器類型" Width="*" IsReadOnly="True" Binding="{Binding strDvrType}"/> <DataGridTextColumn Header="編碼器編碼" Width="2*" IsReadOnly="True" Binding="{Binding strDvrCode}" /> </DataGrid.Columns> </DataGrid> </Grid> </UserControl>

  

  

.Netformwork4.5及以上版本 -------INotifyPropertyChanged,CallerMemberName

命名空間: System.ComponentModel

      System.Runtime.CompilerServices

后台代碼:

        public CvrWnd()
        {
            InitializeComponent();
        }

        private void InitInfo()
        {
            for (int i = 0; i < 10; i++)
            {
                CvrInfo cvrInfo = new CvrInfo();
                cvrInfo.strCvrName = "磁盤陣列" + i.ToString();
                cvrInfo.strCvrIP = "10.10.13.10" + i.ToString();
                cvrInfo.strCvrPort = "506" + i.ToString();
                cvrInfo.strCvrType = "Type " + i.ToString();
                cvrInfo.strCvrCode = "340201030002150000" + i.ToString();
                cvrInfoList.Items.Add(cvrInfo);
            }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            cvrInfoList.Items.Clear();
            InitInfo();
        }

        //磁盤陣列信息
        public class CvrInfo : INotifyPropertyChanged
        {
            private string m_strCvrIP = string.Empty;
            private string m_strCvrPort = string.Empty;
            private string m_strCvrName = string.Empty;
            private string m_strCvrType = string.Empty;
            private string m_strCvrCode = string.Empty;

            public string strCvrIP
            {
                set
                {
                    UpdateProperty(ref m_strCvrIP, value);
                }
                get
                {
                    return m_strCvrIP;
                }
            }
            public string strCvrPort
            {
                set
                {
                    UpdateProperty(ref m_strCvrPort, value);
                }
                get
                {
                    return m_strCvrPort;
                }
            }
            public string strCvrName
            {
                set
                {
                    UpdateProperty(ref m_strCvrName, value);
                }
                get
                {
                    return m_strCvrName;
                }
            }
            public string strCvrType
            {
                set
                {
                    UpdateProperty(ref m_strCvrType, value);
                }
                get
                {
                    return m_strCvrType;
                }
            }
            public string strCvrCode
            {
                set
                {
                    UpdateProperty(ref m_strCvrCode, value);
                }
                get
                {
                    return m_strCvrCode;
                }
            }

            private void UpdateProperty<T>(ref T properValue, T newValue, [CallerMemberName] string propertyName = "")
            {
                if (object.Equals(properValue, newValue))
                {
                    return;
                }
                properValue = newValue;

                OnPropertyChanged(propertyName);
            }

            public event PropertyChangedEventHandler PropertyChanged;
            protected void OnPropertyChanged([CallerMemberName]string propertyName = "")
            {
                PropertyChangedEventHandler handler = PropertyChanged;
                if (handler != null)
                {
                    handler(this, new PropertyChangedEventArgs(propertyName));
                }
            }


        }
    }

  前台代碼:

    <Grid>
        <DataGrid Name="cvrInfoList" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTextColumn Header="磁盤陣列名" Width="*" Binding="{Binding strCvrName}" />
                                        //strCvrName 為CvrInfo.strCvrName <DataGridTextColumn Header="磁盤陣IP" Width="*" Binding="{Binding strCvrIP}" /> <DataGridTextColumn Header="磁盤陣Port" Width="*" Binding="{Binding strCvrPort}" /> <DataGridTextColumn Header="磁盤陣類型" Width="*" Binding="{Binding strCvrType}" /> <DataGridTextColumn Header="磁盤陣編碼" Width="*" Binding="{Binding strCvrCode}" /> </DataGrid.Columns> </DataGrid> </Grid>

  以上兩種方法都可以進行數據綁定(datagrid, treeview等控件都行), 界面刷新, 第一種方法是在.netfromwork中的4.0的版本支持, 該方法的缺點是當類的屬性多了, 如果后期突然改動一下OnPropertyChanged(value)中的value, 界面就不能同步, 而且感覺每個屬性都要那么一行代碼總感覺怪怪的, 第二種方法是在.netformwork4.5版本支持,避過了方法一種所有的缺點,GG


免責聲明!

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



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