Binding constantly updated string with textbox

前端 未结 2 331
温柔的废话
温柔的废话 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:33

    In threadFunc() function, you set the value directly to inputread (lower case), it's a field and have no call to OnPropertyChanged. You can change the code in threadFunc() to InputRead=plc.InputImage[1].ToString(); I hope it works for you.

    0 讨论(0)
  • 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

    <TextBlock Text="{Binding Path=Inputed}"/>
    
    0 讨论(0)
提交回复
热议问题