c# - C#中的数据绑定(bind)问题

标签 c# .net winforms data-binding

我试图更好地理解数据绑定(bind)在 .net 中的工作原理。我正在检查 this文章,我想出了这段代码:

public partial class Form1 : Form//, INotifyPropertyChanged
{
    public event PropertyChangedEventHandler MyTextChanged;

    [System.ComponentModel.Bindable(true)]
    public string MyText
    {
        get { return textBox1.Text; }
        set 
        {
            textBox1.Text = value;
            if (MyTextChanged != null)
                MyTextChanged(this, new PropertyChangedEventArgs("MyText"));
        }
    }

    MyClass myClass { get; set; }

    public Form1()
    {
        InitializeComponent();
        myClass = new MyClass();
        Binding binding = new Binding("MyText", myClass, "Dic");
        binding.Parse += new ConvertEventHandler(binding_Parse);
        binding.Format += new ConvertEventHandler(binding_Format);
        DataBindings.Add(binding);
        myClass.AddStuff("uno", "UNO");
    }

    void OnMyTextChanged(PropertyChangedEventArgs e)
    {
        if (MyTextChanged != null) MyTextChanged(this, e);
    }

    void binding_Format(object sender, ConvertEventArgs e)
    {
        if (e.Value is Dictionary<string, string>)
        {
            Dictionary<string, string> source = (Dictionary<string, string>)e.Value;
            e.Value = source.Count.ToString();
        }
    }

    void binding_Parse(object sender, ConvertEventArgs e)
    {
        MessageBox.Show(e.DesiredType.ToString());
    }

    private void changemyClassButton_Click(object sender, EventArgs e)
    {
        myClass.AddStuff(myClass.Dic.Count.ToString(), "'" + myClass.Dic.Count.ToString() + "'");
    }

    private void changeMyTextButton_Click(object sender, EventArgs e)
    {
        MyText = "1234";
    }
}

public class MyClass : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public Dictionary<string, string> Dic { get; set; }

    public MyClass()
    {
        Dic = new Dictionary<string, string>();
    }

    public void AddStuff(string key, string value)
    {
        Dic.Add(key, value);
        if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Dic"));
    }
}

我正在尝试将 MyText 绑定(bind)到 myClass。问题是永远不会调用函数 binding_Parse。我知道我可以将 textBox1.Text 直接绑定(bind)到 myClass,或者可能有上千种其他可能的方法来做我想做的事情,但这是只是一个练习;我试图了解更好的数据绑定(bind)。所以我想将一个自定义对象绑定(bind)到一个自定义属性,这样我就可以看到从头到尾的过程。自定义对象是myClass,自定义属性是MyText。我已经尝试了各种变体,比如实现 INotifyPropertyChanged,但我无法调用 binding_Parse(我希望它在我调用 changeMyTextButton_Click).我错过了什么吗?

编辑: 简单来说:我想编写一个带有属性 string MyText 的用户控件,然后用户可以绑定(bind)到其他内容,就像绑定(bind) TextBox 一样s Text 属性到其他东西。所以我不想将控件的属性绑定(bind)到对象,我想编写一个带有属性的控件,然后用户可以将其绑定(bind)到对象。

最佳答案

好的,我想通了,以防有人遇到同样的问题。我必须创建一个名为 MyTextChanged 的事件处理程序,让 Binding 知道 MyText 正在更改,并设置 Binding s DataSourceUpdateMode 属性到 OnPropertyChanged。使用这个简单的原理,我可以将屏幕中的一个像素绑定(bind)到宇宙的其余部分 :)。这是代码:

public partial class Form1 : Form
{
    public event EventHandler MyTextChanged;

    [Bindable(true)]
    public string MyText
    {
        get { return textBox1.Text; }
        set 
        {
            if (textBox1.Text != value)
            {
                textBox1.Text = value;
                OnMyTextChanged();
            }
        }
    }

    MyClass myClass { get; set; }

    public Form1()
    {
        InitializeComponent();
        myClass = new MyClass();
        Binding binding = new Binding("MyText", myClass, "Dic");
        binding.DataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged;
        binding.Parse += new ConvertEventHandler(binding_Parse);
        binding.Format += new ConvertEventHandler(binding_Format);
        DataBindings.Add(binding);
        myClass.AddStuff("uno", "UNO");
    }

    void OnMyTextChanged()
    {
        if (MyTextChanged != null) MyTextChanged(this, EventArgs.Empty);
    }

    void binding_Format(object sender, ConvertEventArgs e)
    {
        if (e.Value is Dictionary<string, string>)
        {
            Dictionary<string, string> source = (Dictionary<string, string>)e.Value;
            e.Value = source.Count.ToString();
        }
    }

    void binding_Parse(object sender, ConvertEventArgs e)
    {
        MessageBox.Show(e.DesiredType.ToString());

    }

    private void changemyClassButton_Click(object sender, EventArgs e)
    {
        myClass.AddStuff(myClass.Dic.Count.ToString(), "'" + myClass.Dic.Count.ToString() + "'");
    }

    private void changeMyTextButton_Click(object sender, EventArgs e)
    {
        MyText = "1234";
    }
}

public class MyClass : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public Dictionary<string, string> Dic { get; set; }

    public MyClass()
    {
        Dic = new Dictionary<string, string>();
    }

    public void AddStuff(string key, string value)
    {
        Dic.Add(key, value);
        if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Dic"));
    }
}

关于c# - C#中的数据绑定(bind)问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3991346/

相关文章:

c# - 在命令行使用 aspnet_compiler 抑制编译器警告

c# - 有没有办法在 C# 中使用 Label 对象创建行分隔符?

.net - 将 CSS 类添加到 Html.BeginForm()

c# - 我可以将 Visual Studio 设置为卸载所有项目,而不是选择 1 个项目并仅递归加载其依赖项吗?

c# - 使用 LINQ 并行处理 csv

c# - 获取非 GDI 应用程序中的插入符位置

c# - 存储网络服务的密码?

c# - 如何检测当前按下的键?

c# - 如果已经打开则关闭子窗体

c# - 如何要求用户在textbox1中输入数组中的数字并在textbox2中显示