用文本框绑定不断更新的字符串 [英] Binding constantly updated string with textbox

查看:28
本文介绍了用文本框绑定不断更新的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望将字符串与文本框绑定.该字符串在一个线程中不断更新:

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

String inputread;

    public event PropertyChangedEventHandler PropertyChanged;

    public string InputRead
    {
        get { return inputread; }
        set
        {
            if (Equals(inputread, value) == true) return;
            inputread = value;
            this.OnPropertyChanged(nameof(this.inputread));
        }


    }
    void threadFunc()
    {
        try
        {
            while (threadRunning)
            {
                plc.Read();
                InputRead =plc.InputImage[1].ToString();
                MessageBox.Show(InputRead);
            }
        }
        catch (ThreadAbortException)
        {
        }
    }
    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }

后面的绑定声明:

Binding bind = new Binding("InputRead");
bind.Mode = BindingMode.OneWay;
BindingOperations.SetBinding(newtextbox, TextBox.TextProperty, bind);

我理解为什么这不起作用(文本框完全为空)的部分问题是因为我没有在线程每次运行时刷新它.我该怎么做?此外,我怀疑 Binding 声明有缺陷,我不确定如何.

I understand that part of the problem why this is not working (the textbox is completely empty) is because I do not refresh it every time the thread runs. How do I do it? Also I suspect the Binding declaration is flawed I am not sure how.

我阅读了关于数据绑定的 MSDN 文章,它帮助我走到了这一步.
我在谷歌上搜索了它,这就是我如何做到这一点,同样在早期的 Stackoverflow 的帮助下,仍然没有成功.

I read the MSDN article about data binding, it helped me get this far.
I googled it that is how i got this far, also with the help of Stackoverflow earlier, still no success.

我稍微编辑了代码,文本框仍然是空的(甚至不是 0).我正在使用 wpf!如果更容易,有人可以指导我使用 dispatcher.invoke 吗?

I edited the code a bit, still textbox is empty (not even 0). I am using wpf! If it is easier, could someone guide me to use dispatcher.invoke ?

谢谢!

推荐答案

您需要创建一个属性并将TextBox绑定到该属性

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}"/>

这篇关于用文本框绑定不断更新的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆