Beispiel #1
0
 private static void UnattachDataContext(Control control, object dataContext, RelayInfoCollection collection)
 {
     for (int i = 0; i < control.InputBindings.Count; i++)
     {
         if (control.InputBindings[i] is RelayInfoCommandBinding binding)
         {
             control.InputBindings.RemoveAt(i--);
         }
     }
 }
Beispiel #2
0
        private static void OnControlDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            Control control = (Control)sender;

            // If not attached, do nothing
            RelayInfoCollection collection = GetCollection(control);

            if (collection == null)
            {
                return;
            }

            if (e.OldValue != null)
            {
                UnattachDataContext(control, e.OldValue, collection);
            }
            if (e.NewValue != null)
            {
                AttachDataContext(control, e.NewValue, collection);
            }
        }
Beispiel #3
0
        private static void AttachDataContext(Control control, object dataContext, RelayInfoCollection collection)
        {
            Type dataType = dataContext.GetType();

            foreach (RelayInfo info in collection)
            {
                PropertyInfo prop = dataType.GetProperty(info.Name);
                if (prop == null)
                {
                    throw new ArgumentNullException($"No property found with the name \"{info.Name}\"!");
                }
                //if (!typeof(ICommand).IsAssignableFrom(prop.PropertyType))
                //	throw new ArgumentNullException($"Property \"{info.Name}\" is not a type of {nameof(ICommand)}!");
                IRelayInfoCommand command = (IRelayInfoCommand)prop.GetValue(dataContext);
                command.Info = info;
                if (command.InputGesture != null)
                {
                    control.InputBindings.Add(new RelayInfoCommandBinding(command));
                }
            }
        }
Beispiel #4
0
 public static void SetCollection(DependencyObject d, RelayInfoCollection value)
 {
     d.SetValue(CollectionProperty, value);
 }