Databinding to a method in WPF

前端 未结 5 1199
失恋的感觉
失恋的感觉 2021-02-13 13:49

I am having trouble databinding a TextBox.Text property to a object\'s method. The idea is allowing the user to write in a TextBox a file name and then

5条回答
  •  借酒劲吻你
    2021-02-13 14:14

    Databinding requires the NotifyPropertyChanged event to be called when the source is updated. In this case, you'd want to wrap this function call in a get/set like so:

    
    
    public class FileWrapper: System.ComponentModel.INotifyPropertyChanged{
        private string m_filename;
    
        public string FileExtension{
            get{ return GetFileExtension(FileName);}
        }
    
        public string FileName{
            get{ return m_filename;}
            set{ m_filename = value; OnPropertyChanged("FileName"); OnPropertyChanged( "FileExtension");
        }
    
        public string GetFileExtension( string filename){
            //implementation
        }
    
        public event System.ComponentModel.NotifyPropertyChangedEvent PropertyChanged = (a,b)=>{};
    
        protected void OnPropertyChanged(string property){
            PropertyChanged( this, new System.ComponentModel.PropertyChangedEventArgs( property ));
        }
    }
    
    

提交回复
热议问题