2

Hi I am in the process of writing my first .net gui. I am wondering if there is some specific way I need to apply to my poco objects for them to be bindable to a usercontrol. I have a few objects but I seem to be unable to bind them to my usercontrol.

I read somewhere that they need to implement IBindable but I can't shake the feeling that someone already has eliminated all that duplicate code I would have to input into all my classes. Is there a way to easily bind these or would I have to use datasets or the like to be easily get this binding working. I have an extreme distaste for datasets to please present some other decent options ;-)

I am trying to bind to usercontrols from the devexpress toolkit.

4

3 回答 3

1

哪种架构?

对于单向绑定,除了公共属性之外,您不需要任何其他东西 - 可能还有任何定制数据类型( s 等)的一些TypeConverter实现struct

对于完整的 2 路绑定,您需要一个事件实现 - 任何一个:

  • 每个属性“Foo”的“公共事件 EventHandler FooChanged”
  • 一个`INotifyPropertyChanged 实现
  • 一个定制的组件模型(不要去那里 - 矫枉过正)

对于INotifyPropertyChanged实现的示例(请注意,您可能希望移动一些代码以供重用):

public class Foo : INotifyPropertyChanged
{
    private string bar;
    public string Bar
    {
        get { return bar; }
        set { UpdateField(ref bar, value, "Bar"); }
    }
    // other properties...

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null) PropertyChanged(this,
            new PropertyChangedEventArgs(propertyName));
    }
    protected bool UpdateField<T>(ref T field, T value,
        string propertyName)
    {
        if (!EqualityComparer<T>.Default.Equals(field, value))
        {
            field = value;
            OnPropertyChanged(propertyName);
            return true;
        }
        return false;
    }
}

要绑定数据(网格等),最简单的方法是使用泛型;基本上,最小值IList- 但你从public T this[int index]索引器获得额外的元数据 -List<T>等等Collection<T>都有。更多 -BindingList<T>实现IBindingList允许基于集合的通知事件(但仅限于INotifyPropertyChanged- 不是FooChanged模式)。

于 2009-02-08T15:40:46.557 回答
0

使用 WPF,您将需要INotifyPropertyChangedDependency Properties。还可以查看INotifyCollectionChanged收藏。

于 2009-02-08T15:11:00.797 回答
0

BindingList可用于绑定到通用对象列表。

于 2009-02-08T15:25:13.030 回答