1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
public abstract class RibbonControlWrapper
{
protected RibbonControl _Control;
public RibbonControlWrapper(RibbonControl control)
{
_Control = control;
}
protected void DataBind(string propertyName, object dataSource, string dataMember, string eventName)
{
var sourceProperty = dataSource.GetType().GetProperty(dataMember);
var controlProperty = _Control.GetType().GetProperty(propertyName);
_Control.GetType().GetEvent(eventName).AddEventHandler(_Control,
new EventHandler<RibbonControlEventArgs>((s, e) =>
{
sourceProperty.SetValue(dataSource, Convert.ChangeType(controlProperty.GetValue(_Control, null), sourceProperty.PropertyType), null);
}));
System.Action updateControl = () =>
{
controlProperty.SetValue(_Control, Convert.ChangeType(sourceProperty.GetValue(dataSource, null), controlProperty.PropertyType), null);
};
if (dataSource is INotifyPropertyChanged)
((INotifyPropertyChanged)dataSource).PropertyChanged += new PropertyChangedEventHandler((s, e) =>
{
updateControl();
});
updateControl();
}
}
public class RibbonEditBoxWrapper : RibbonControlWrapper
{
public RibbonEditBoxWrapper(RibbonEditBox control) : base(control) { }
public void DataBind(object dataSource, string dataMember)
{
DataBind("Text", dataSource, dataMember, "TextChanged");
}
} |
Partager