bool作为CheckBox的数据源(bool在其影响的线程之外的其他线程中更新)

本文关键字:线程 bool 更新 其他 CheckBox 作为 数据源 影响 | 更新日期: 2023-09-27 18:02:32

我试图找到如何分配一个bool复选框。我想让它成为复选框。当我的bool数据改变时,检查值会自动刷新。我知道,对于ComboBox,有DataSource属性,使其与列表,但我无法找到与checkbox等效的。

我尝试使用复选框。数据绑定,但它似乎不起作用。另一方面,我真的不知道第三个属性是什么意思。

checkBox.DataBindings.Add("Checked", DisableBugWatcher, "check");

我需要这个,因为我有两个独立的窗口刷新相同的复选框值!

EDIT:

我尝试使用事件来更新我的主GUI,但它说:跨线程操作无效:控制'checkBox'从线程访问,而不是创建它的线程。

问题是链接到一个事实,即bool值是从另一个线程刷新的,而不是它所影响的线程。

bool作为CheckBox的数据源(bool在其影响的线程之外的其他线程中更新)

要使DataBindings工作,您必须在包含bool值的类中实现INotifyPropertyChange。如果你从UI线程以外的线程命中myValue,你必须使用SynchronizationContext并在UI线程中初始化myObject。

public class myObject : INotifyPropertyChanged
{
    // The class has to be initialized from the UI thread
    public SynchronizationContext context = SynchronizationContext.Current;
    bool _myValue;
    public bool myValue
    {
        get
        {
            return _myValue;
        }
        set
        {
            _myValue = value;
            // if (PropertyChanged != null)
                // PropertyChanged(this, new PropertyChangedEventArgs("myValue"));
            if (PropertyChanged != null)
            {
                context.Send(
                    new SendOrPostCallback(o => 
                       PropertyChanged(this, new PropertyChangedEventArgs("myValue"))
                    ), null);
            }
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
}

然后,设置你的DataBinding如下:

checkBox1.DataBindings.Add("Checked", myObject.GlobalObject, "myValue");

第一个参数是要绑定到的UI对象的属性。第二个属性是目标对象实例,第三个属性是需要绑定到第一个属性的目标对象的属性名。

我尽了最大的努力来反映你的场景,使用一个计时器,每秒切换myValue(相应地检查复选框)。下面是我使用的表单代码:

System.Timers.Timer x = new System.Timers.Timer();
myObject target;
public Form1()
{
    InitializeComponent();
    target = new myObject();
    x.Elapsed += (s, e) =>
        {
            target.myValue = !target.myValue;
        };
    x.Interval = 1000;
    checkBox1.DataBindings.Add("Checked", target, "myValue");
    x.Start();
}

我和朋友找到了一个更简单的方法来解决我的问题。我从我的表单中使用了对话结果。当我从表单返回时它会给我按钮点击的状态并给我文本框的值。相同的代码适用于我的复选框问题。

下面是一个示例代码:
public partial class MainWindow : Form
{
    private OtherWindow m_otherWindow;
    public MainWindow()
    {
        InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e)
    {
        // Lazy create other window if it doesn't exist.
        m_otherWindow = m_otherWindow ?? new OtherWindow();
        // Passe textbox value to other window.
        m_otherWindow.PassedValue=textBox1.Text;
        if (m_otherWindow.ShowDialog()==DialogResult.OK)
        {
            // Clicked ok : update textbox value with textbox value of other window.
            textBox1.Text=m_otherWindow.PassedValue;
        }
    }
    private void button2_Click(object sender, EventArgs e)
    {
        Close();
    }
}
public partial class OtherWindow : Form
{
    /// <summary>
    /// Value to be passed to the window.
    /// </summary>
    public string PassedValue
    {
        get { return textBox1.Text; }
        set { textBox1.Text = value; }
    }
    public OtherWindow()
    {
        InitializeComponent();
    }
    private void button2_Click(object sender, EventArgs e)
    {
        DialogResult = DialogResult.OK;
    }
}