PropertyChangedEventHandler为null

时间:2012-03-19 03:50:33

标签: c# winforms data-binding

我所拥有的是一个简单的包装器,它围绕一个实现INotifyPropertyChanged接口的Vector3类(由OpenTK提供的三维矢量)。我希望有三个文本框绑定到此向量(x,y和z坐标)。我的包装器(CameraVector)看起来像:

public class CameraVector : INotifyPropertyChanged
    {
    public event PropertyChangedEventHandler PropertyChanged;
    private Vector3 m_vector;

    public CameraVector ()
        {
        m_vector = new Vector3 ();
        }

    public CameraVector (Vector3 vector)
        {
        m_vector = vector;
        }

    public CameraVector (float x, float y, float z)
        {
        m_vector = new Vector3 (x, y, z);
        }

    public String CoordX
        {
        get { return Convert.ToString (m_vector.X); }
        set 
            {
            if (CoordX != value)
                {
                m_vector.X = Convert.ToSingle (value);
                OnPropertyChanged (new PropertyChangedEventArgs ("CoordX"));
                }
            }
        }

    public String CoordY
        {
        get { return Convert.ToString (m_vector.Y); }
        set
            {
            if (CoordY != value)
                {
                m_vector.Y = Convert.ToSingle (value);
                OnPropertyChanged (new PropertyChangedEventArgs ("CoordY"));
                }
            }
        }

    public String CoordZ
        {
        get { return Convert.ToString (m_vector.Z); }
        set
            {
            if (CoordZ != value)
                {
                m_vector.Z = Convert.ToSingle (value);
                OnPropertyChanged (new PropertyChangedEventArgs ("CoordZ"));
                }
            }
        }

    public Vector3 Vector
        {
        get { return m_vector; }
        set 
            {
            CoordX = Convert.ToString (value.X);
            CoordY = Convert.ToString (value.Y);
            CoordZ = Convert.ToString (value.Z);
            }
        }

    protected void OnPropertyChanged (PropertyChangedEventArgs e)
        {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
            {
            handler (this, e);
            }
        }
    }

这为要绑定的TextBox提供了三个属性,同时允许应用程序的其他区域直接传递(和接收)Vector3。在包含的形式我有:

m_center = new CameraVector (0.0f, 0.0f, 0.0f);
txtCameraX.DataBindings.Add ("Text", m_center, "CoordX");
txtCameraY.DataBindings.Add ("Text", m_center, "CoordY");
txtCameraZ.DataBindings.Add ("Text", m_center, "CoordZ");

问题是当我进入我的OnPropertyChanged方法时,事件处理程序为null。我的理解是,这就是数据绑定如何了解变化。那就是说,我在这里做错了吗?我对C#中的数据绑定非常陌生,所以任何批评都是受欢迎的。

编辑:根据以下建议,我已经这样做了:

m_bindCenter.DataSource = m_center;
txtCameraX.DataBindings.Add ("Text", m_bindCenter, "CoordX", false, DataSourceUpdateMode.OnPropertyChanged);
txtCameraY.DataBindings.Add ("Text", m_bindCenter, "CoordY", false, DataSourceUpdateMode.OnPropertyChanged);
txtCameraZ.DataBindings.Add ("Text", m_bindCenter, "CoordZ", false, DataSourceUpdateMode.OnPropertyChanged);

1 个答案:

答案 0 :(得分:0)

BindingSource挂起INotifyProperty更改了处理程序(如果列表中的属性发生更改,BindingList会自动为您引发ListChanged事件。)

创建一个BindingSource,将其DataSource属性设置为m_center。

创建绑定时,将BindingSource作为第二个参数传递,而不是m_center。

INotifyPropertyChanged文档实际上有一个例子。他们使用BindingList,但它应该指向正确的方向。