Binding constantly updated string with textbox

前端 未结 2 352
温柔的废话
温柔的废话 2021-01-26 13:08

I wish to bind a String with a textbox. The string is constantly being updated in a thread:

String inputread;

    public event PropertyChangedEventHandler Prope         


        
2条回答
  •  無奈伤痛
    2021-01-26 13:42

    You need create a property and bind TextBox to the property

    private string _Inputed;
    public string Inputed
    {
        get { return _Inputed; }
        set 
        {
            if(Equals(_Inputed, value) == true) return;
            _Inputed = value;
            this.OnPropertyChanged(nameof(this.Inputed));
        }
    }
    
    void threadFunc()
    {
        try
        {
            while (threadRunning)
            {
                plc.Read();
                this.Inputed = plc.InputImage[1].ToString();
            }
        }
        catch (ThreadAbortException)
        {
        }
    }
    
    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
    

    XAML

    
    

提交回复
热议问题