public Connector(IDataSource dataSource, ICommandSink command) { _dataSource = dataSource; _commandSink = command; _dataSource.DataUpdated += OnDataUpdated; }
static void OnCommandSinkChanged(DependencyObject depObj, DependencyPropertyChangedEventArgs e) { ICommandSink commandSink = e.NewValue as ICommandSink; if (!ConfigureDelayedProcessing(depObj, commandSink)) { ProcessCommandSinkChanged(depObj, commandSink); } }
static void ProcessCommandSinkChanged(DependencyObject depObj, ICommandSink commandSink) { CommandBindingCollection cmdBindings = GetCommandBindings(depObj); if (cmdBindings == null) { throw new ArgumentException("The CommandSinkBinding.CommandSink attached property was set on an element that does not support CommandBindings."); } foreach (CommandBinding cmdBinding in cmdBindings) { CommandSinkBinding csb = cmdBinding as CommandSinkBinding; if (csb != null && csb.CommandSink == null) { csb.CommandSink = commandSink; } } }
// This method is necessary when the CommandSink attached property is set on an element // in a template, or any other situation in which the element's CommandBindings have not // yet had a chance to be created and added to its CommandBindings collection. static bool ConfigureDelayedProcessing(DependencyObject depObj, ICommandSink commandSink) { bool isDelayed = false; CommonElement elem = new CommonElement(depObj); if (elem.IsValid && !elem.IsLoaded) { RoutedEventHandler handler = null; handler = delegate { elem.Loaded -= handler; ProcessCommandSinkChanged(depObj, commandSink); }; elem.Loaded += handler; isDelayed = true; } return isDelayed; }
// This method is necessary when the CommandSink attached property is set on an element // in a template, or any other situation in which the element's CommandBindings have not // yet had a chance to be created and added to its CommandBindings collection. static bool ConfigureDelayedProcessing(DependencyObject depObj, ICommandSink commandSink) { bool isDelayed = false; CommonElement elem = new CommonElement(depObj); if (elem.IsValid && !elem.IsLoaded) { RoutedEventHandler handler = null; handler = delegate { elem.Loaded -= handler; ProcessCommandSinkChanged(depObj, commandSink); }; elem.Loaded += handler; isDelayed = true; } return(isDelayed); }
public static void SetCommandSink(DependencyObject obj, ICommandSink value) { obj.SetValue(CommandSinkProperty, value); }
static void ProcessCommandSinkChanged(DependencyObject depObj, ICommandSink commandSink) { CommandBindingCollection cmdBindings = GetCommandBindings(depObj); if (cmdBindings == null) throw new ArgumentException("The CommandSinkBinding.CommandSink attached property was set on an element that does not support CommandBindings."); foreach (CommandBinding cmdBinding in cmdBindings) { CommandSinkBinding csb = cmdBinding as CommandSinkBinding; if (csb != null && csb.CommandSink == null) csb.CommandSink = commandSink; } }
public WhenConnectingDataSourceAndCommandSink() { _dataSource = A.Fake <IDataSource>(); _commandSink = A.Fake <ICommandSink>(); _connector = new Connector(_dataSource, _commandSink); }