数据绑定是否支持 Windows 窗体中的嵌套属性? [英] Does data binding support nested properties in Windows Forms?

查看:27
本文介绍了数据绑定是否支持 Windows 窗体中的嵌套属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Windows 窗体中编写测试应用程序.它有一个带有 TextBox 的简单表单,需要实现 DataBinding.我已经实现了 FormViewModel 类来保存我的数据,并为我的业务数据提供了 1 个类 — TestObject.

I am writing the test application in Windows Forms. It has a simple form with TextBox and needs to implement DataBinding. I have implemented the class FormViewModel to hold my data, and have 1 class for my business data — TestObject.

业务数据对象:

public class TestObject : INotifyPropertyChanged
{
    private string _testPropertyString;
    public string TestPropertyString
    {
        get
        {
            return _testPropertyString;
        }
        set
        {
            if (_testPropertyString != value)
            {
                _testPropertyString = value;
                RaisePropertyChanged("TestPropertyString");
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    public void RaisePropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

视图模型:

public class FormViewModel : INotifyPropertyChanged
{
    private TestObject _currentObject;
    public TestObject CurrentObject
    {
        get { return _currentObject; }
        set
        {
            if (_currentObject != value)
            {
                _currentObject = value;

                RaisePropertyChanged("CurrentObject");
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    public void RaisePropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

属性:

private FormViewModel _viewModel;
public FormViewModel ViewModel
{ 
    get
    {
        if (_viewModel == null)
            _viewModel = new FormViewModel();

        return _viewModel;
    }
}

所以现在我正在尝试将我的数据绑定到 TextBox,如下所示:

So now I'm trying to bind my data to TextBox like this:

TextBox.DataBindings.Add("Text", ViewModel, "CurrentObject.TestPropertyString");

令人惊讶的是,它不起作用!当我更改 CurrentObject 或更改 TestPropertyString 属性时,没有任何变化.

And surprisingly, it doesn't work! Nothing changes, when I change CurrentObject, or change TestPropertyString property.

但是当我使用时效果很好:

But it works great, when I use:

TextBox.DataBindings.Add("Text", ViewModel.CurrentObject, "TestPropertyString");

<小时>

所以我的问题是:数据绑定是否支持嵌套属性?

谢谢解释!

推荐答案

Databinding 行为在 .NET 4.0 中发生了变化.您的代码适用于 .NET 3.5.我在 Microsoft Connect 上发现了这个问题:.Net 4.0简单绑定问题

The Databinding behavior was changed in .NET 4.0. Your code works on .NET 3.5. I found this issue posted at Microsoft Connect: .Net 4.0 simple binding issue

这是对我有用的解决方法.使用 BindingSource 作为数据对象:

Here was the work-around that worked for me. Use a BindingSource as the data object:

BindingSource bs = new BindingSource(_viewModel, null);

//textBox1.DataBindings.Add("Text", _viewModel, "CurrentObject.TestPropertyString");
textBox1.DataBindings.Add("Text", bs, "CurrentObject.TestPropertyString");

这篇关于数据绑定是否支持 Windows 窗体中的嵌套属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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