Is it possible to databind to an extension method?

一个人想着一个人 提交于 2019-12-06 01:33:57

When you are passed the list of Transmission objects you can use the Façade pattern and store them in a container designed...

public class TransmissionContainer : INotifyPropertyChanged
{
    private readonly Transmission _transmission;
    public TransmissionContainer(Transmission transmission)
    {
        _transmission = transmission;
    }
    private int _id;
    public int Id
    {
        [DebuggerStepThrough]
        get { return _transmission.ID; }
        [DebuggerStepThrough]
        set
        {
            if (value != _transmission.ID)
            {
                _transmission.ID = value;
                OnPropertyChanged("Id");
            }
        }
    }
    public bool Cancelled
    {
        [DebuggerStepThrough]
        get { return _transmission.Cancelled }
        [DebuggerStepThrough]
        set
        {
            if (value != _transmission.Cancelled)
            {
                _transmission.Cancelled = value;
                OnPropertyChanged("Cancelled");
                OnPropertyChanged("Status");
            }
        }
    }
    public string Status
    {
        [DebuggerStepThrough]
        get
        {
            StringBuilder sb = new StringBuilder("|");
            if (_transmission.Cancelled)
                sb.Append("| Cancelled ");
            if (_transmission.Recorded)
                sb.Append("| Recorded ");
            if (_transmission.Stored)
                sb.Append("| Stored ");
            sb.Append("||");
            return sb.ToString();
        }
    }
    //
    // code in other properties here
    //
    #region INotifyPropertyChanged Implementation
    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(string name)
    {
        var handler = System.Threading.Interlocked.CompareExchange(ref PropertyChanged, null, null);
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
    #endregion
}

This is a container class that creates a façade to which your Xaml can bind transparently. As shown, each of the exposed properties simply echoes back the value in the private instance of Transmission. Changes are relayed to the WPF binding engine via the INotifyPropertyChanged interface.

To create an instance you can construct it with the original Transmission class. To bind a collection of these containers, you can declare an ObservableCollection of type TransmissionContainer. Doing that means the list is bound in addition to the various changes in the properties.

In this approach, your 'extension' property is just another exposed property that has no setter. Note that changes to the other members that affect Status call the notification on behalf of the 'extension' property. Coding in the remaining members of the Transmission class should take about 20 minutes...

That is what you usually use a pattern like MVVM for. You add properties to the view-model which are based on the model and only relevant to the view. The view-model can contain a reference to the model to either bind directly to its properties or to mirror them on the view-model (for decoupling i would opt for the latter).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!