简体   繁体   English

如何以编程方式在 C# 中指定单向绑定?

[英]How to programmatically specify one-way binding in C#?

Simple C# question, but I have been unable to find the answer, despite much searching.简单的 C# 问题,但我一直找不到答案,尽管搜索了很多。 (I think I know how it works in WPF XAML file, but I am using WinForms and binding programmatically). (我想我知道它在 WPF XAML 文件中是如何工作的,但我正在使用 WinForms 并以编程方式绑定)。 This must be simple.这一定很简单。 How do I tell it to bind one-way?我如何告诉它单向绑定?

In this case, chkUnchkAllChkBx is a check box.在这种情况下,chkUnchkAllChkBx 是一个复选框。 AllCheckBoxesChecked is boolean = true if all checkboxes in a certain panel are checked.如果选中某个面板中的所有复选框,则 AllCheckBoxesChecked 为 boolean = true。 (Content of panel is not known in advance). (事先不知道面板的内容)。

-Aram -阿拉姆

        public AllCheckBoxesChecked panelCheckBoxesChecked = new AllCheckBoxesChecked();        
    chkUnchkAllChkBx.DataBindings.Add("Checked",panelCheckBoxesChecked,"AllChecked");
    
    public class AllCheckBoxesChecked : INotifyPropertyChanged
    { private bool _allChecked;

        public AllCheckBoxesChecked()
        {
            _allChecked = false;
        }
        public bool AllChecked
        {
            get { return _allChecked; }
            set
            {
                _allChecked = value;
                OnPropertyChanged("AllChecked");
            }
        }
       public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string info)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(info));
            }
        }
    } 

There is no equivalent in WinForms of WPF's BindingMode. WPF 的 BindingMode 的 WinForms 中没有等效项。 So let's clarify the issue.所以让我们澄清一下这个问题。 There should only be a problem if, when value of AllCheckBoxesChecked.AllChecked is changed by changing the value of the bound CheckBox chkUnchkAllChkBx, there is an unwelcome effect in the application, such as if the values of all the CheckBoxes in the 'certain' panel were to be changed to the same value as chkUnchkAllChkBx.只有当 AllCheckBoxesChecked.AllChecked 的值通过更改绑定的 CheckBox chkUnchkAllChkBx 的值而更改时,才会出现问题,在应用程序中会产生不受欢迎的效果,例如“某些”面板中所有 CheckBox 的值将被更改为与 chkUnchkAllChkBx 相同的值。

Assuming you do have a problem along those lines, you just need to make sure that the way the binding class AllCheckBoxesChecked is used does not cause problems.假设您确实遇到了这些问题,您只需要确保使用绑定 class AllCheckBoxesChecked 的方式不会导致问题。 A simple way that might work, depending on your application, would be to make AllCheckBoxesChecked.AllChecked a get-only property, with the value of its backing field set by a method.根据您的应用程序,一种可能有效的简单方法是使 AllCheckBoxesChecked.AllChecked 成为仅获取属性,其支持字段的值由方法设置。 Something like this:像这样的东西:

public bool AllChecked
    {
        get { return _allChecked; }
    }

public void SetAllChecked(bool value)
    {
        _allChecked = value;
        OnPropertyChanged("AllChecked");
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM