如何使用 WinForms 数据绑定正确触发用户控件中值的更改



我在WinForms应用程序中创建了两个UserControl。一个包含一个TextBox(现在让我们称之为TextEntryControl),另一个应该使用我在此TextBox中输入的值来执行内部操作(启用按钮并在单击此按钮时使用该值) - 让我们称之为TextUsingControl

但是,我不能通过数据绑定做到这一点。

第一种(幼稚的)方法

我像这样向TextEntryControl添加了一个string属性:

public string MyStringProperty { get; set; }

然后我使用UI设计器将TextBoxText属性绑定到这个MyStringProperty,导致textEntryControlBindingSource。我在InitializeComponents()后面添加了构造函数:

textEntryControlBindingSource.Add(this);

我将相同的属性添加到TextUsingControl,并且在使用两个控件的外部 UI 中,我将TextUsingControl的字符串属性绑定到其中一个TextEntryControl,并相应地更新了绑定源:

textEntryControlBindingSource.Add(textEntryControl1);

我在TabControl的不同选项卡上使用这两个控件,并且当我第一次在文本框中输入文本然后切换到另一个控件时,该机制只工作一次。

下次尝试

我为字符串创建了一个简单的包装类:

public sealed class StringWrapper {
    public string Content { get; set; }
}

在我的文本输入控件中,我将文本框绑定到此字符串包装器,并将属性更改为如下所示:

public string MyStringProperty {
    get {
        return _stringWrapper.Content;
    }
    set {
        _stringWrapper.Content = value;
    }
}

我在外部控件中使用TabControl做了类似的事情 - 使用StringWrapper将两个用户控件的两个MyStringProperty绑定到。

结果:相同。但这是合乎逻辑的,因为委托给包装器的外部属性不会收到通知。

第三次尝试

这个有点有效,但我认为这是一个丑陋的解决方法。

我完全放弃了MyStringProperty,并通过一个属性传入包装对象本身,该属性再次将其传递给绑定源:

public StringWrapper MyStringWrapper {
    get {
        return stringWrapperBindingSource.Cast<StringWrapper>().FirstOrDefault();
    }
    set {
        stringWrapperBindingSource.Clear();
        if(value != null) stringWrapperBindingSource.Add(value);
    }
}

现在我只创建一个StringWrapper对象,并在InitializeComponent()之后立即将其设置为两个用户控件。

不定属性已更改

作为跟进:我尝试了INotifyPropertyChanged以及在MSDN上描述的。这也无济于事。

我想要实现的目标

我希望两个用户控件都有一个MyStringProperty,当我输入TextEntryControl文本框的文本更改时,该属性应更新并正确通知它附加到的任何绑定源。TextUsingControl应在其属性更改时自行更新。

后半部分很容易,我只是将适当的逻辑添加到属性的set部分,但我在处理第一个时遇到了麻烦。

我已经习惯了 Eclipse 的 JFace 数据绑定,其中此功能可以通过 PropertyChangeSupportPropertyChangeListener来实现 - 在这里,我只需将适当的事件触发代码添加到 setter 中,我可以在设置数据绑定时使用BeanProperties.value()

它是正确的属性实现和正确的数据绑定的组合。

(1)财产实施:

该属性不需要很复杂。它可以是简单的类型,就像您的幼稚方法一样,但必不可少的部分是它应该提供属性更改通知。可以使用常规INotifyPropertyChanged机制或特定于 Windows 窗体PropertyNameChanged命名事件模式。在这两种情况下,您都无法使用 C# auto 属性功能,而必须手动实现它(使用显式支持字段)。下面是一个示例实现:

string myStringProperty;
public string MyStringProperty
{
    get { return myStringProperty; }
    set
    {
        if (myStringProperty == value) return;
        myStringProperty = value;
        var handler = MyStringPropertyChanged;
        if (handler != null) handler(this, EventArgs.Empty);
    }
}
public event EventHandler MyStringPropertyChanged;

(2)数据绑定:

将单个控件属性绑定到单个对象属性称为简单数据绑定,通过 Control.DataBinding 实现。有关详细信息,可以查看 ControlBindingsCollection.Add 方法/绑定构造函数重载和绑定类属性/方法/事件。

Binding的作用基本上是在源对象属性和目标对象属性之间创建链接(单向或双向)。请注意属性一词 - 这正是开箱即用支持的。但是使用以下简单的帮助程序方法(在我回答如何设置 Textbox.Enabled 从 false 到 true on TextChange 中提出?),您可以轻松创建像绑定这样的单向表达式:

public static void Bind(this Control target, string targetProperty, object source, string sourceProperty, Func<object, object> expression)
{
    var binding = new Binding(targetProperty, source, sourceProperty, true, DataSourceUpdateMode.Never);
    binding.Format += (sender, e) => e.Value = expression(e.Value);
    target.DataBindings.Add(binding);
}

这是一个完整的工作演示:

using System;
using System.Windows.Forms;
namespace Samples
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            var form = new Form();
            var splitView = new SplitContainer { Dock = DockStyle.Fill, Parent = form };
            var textEntry = new TextEntryControl { Dock = DockStyle.Fill, Parent = splitView.Panel1 };
            var textConsumer = new TextConsumingControl { Dock = DockStyle.Fill, Parent = splitView.Panel2 };
            textConsumer.DataBindings.Add("MyStringProperty", textEntry, "MyStringProperty", true, DataSourceUpdateMode.Never);
            Application.Run(form);
        }
    }
    class TextEntryControl : UserControl
    {
        TextBox textBox;
        public TextEntryControl()
        {
            textBox = new TextBox { Parent = this, Left = 16, Top = 16 };
            textBox.DataBindings.Add("Text", this, "MyStringProperty", true, DataSourceUpdateMode.OnPropertyChanged);
        }
        string myStringProperty;
        public string MyStringProperty
        {
            get { return myStringProperty; }
            set
            {
                if (myStringProperty == value) return;
                myStringProperty = value;
                var handler = MyStringPropertyChanged;
                if (handler != null) handler(this, EventArgs.Empty);
            }
        }
        public event EventHandler MyStringPropertyChanged;
    }
    class TextConsumingControl : UserControl
    {
        Button button;
        public TextConsumingControl()
        {
            button = new Button { Parent = this, Left = 16, Top = 16, Text = "Click Me" };
            button.Bind("Enabled", this, "MyStringProperty", value => !string.IsNullOrEmpty(value as string));
        }
        string myStringProperty;
        public string MyStringProperty
        {
            get { return myStringProperty; }
            set
            {
                if (myStringProperty == value) return;
                myStringProperty = value;
                var handler = MyStringPropertyChanged;
                if (handler != null) handler(this, EventArgs.Empty);
            }
        }
        public event EventHandler MyStringPropertyChanged;
    }
    public static class BindingUtils
    {
        public static void Bind(this Control target, string targetProperty, object source, string sourceProperty, Func<object, object> expression)
        {
            var binding = new Binding(targetProperty, source, sourceProperty, true, DataSourceUpdateMode.Never);
            binding.Format += (sender, e) => e.Value = expression(e.Value);
            target.DataBindings.Add(binding);
        }
    }
}

如您所见,textEntrytextConsumer 之间的请求(单向)绑定是用以下一行建立的:

textConsumer.DataBindings.Add("MyStringProperty", textEntry, "MyStringProperty", true, DataSourceUpdateMode.Never);

您可以在演示中看到的另一个有趣的点是,如果您愿意,您实际上也可以数据绑定内部控制属性。TextEntryControl内部文本框和属性之间的整个同步是通过以下一行实现的:

textBox.DataBindings.Add("Text", this, "MyStringProperty", true, DataSourceUpdateMode.OnPropertyChanged);

以及启用TextConsumingControl内部按钮:

button.Bind("Enabled", this, "MyStringProperty", value => !string.IsNullOrEmpty(value as string));

当然,最后两件事是可选的,你可以在属性 setter 中执行此操作,但知道存在这样的选项有点酷。

最新更新