コード例 #1
0
        // Fires when the property to update is changed
        private static void OnTargetPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ContainsItemBehavior behavior = (ContainsItemBehavior)d;

            // Update bindings to reflect the new target property

            if (e.OldValue is string)
            {
                behavior.clearValueBinding((string)e.OldValue);
            }

            if (e.NewValue is string)
            {
                behavior.setValueBinding((string)e.NewValue);
            }
        }
コード例 #2
0
        // Fires when a collection is specified
        private static void OnCollectionPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ContainsItemBehavior behavior = (ContainsItemBehavior)d;

            if (e.NewValue is INotifyCollectionChanged)
            {
                // Validate the new collection to ensure that it has a Contains method
                // with the expected signature
                MethodInfo info = e.NewValue.GetType().GetMethod("Contains");
                if (info == null)
                {
                    throw new ArgumentException(Strings.ContainsErrorMessage);
                }

                if (info.ReturnType != typeof(bool) && info.ReturnType != typeof(bool?))
                {
                    throw new ArgumentException(Strings.ContainsErrorMessage);
                }

                ParameterInfo[] parameters = info.GetParameters();
                if (parameters.Length != 1)
                {
                    throw new ArgumentException(Strings.ContainsErrorMessage);
                }

                // Store the type of item the Contains method expects
                behavior._expectedItemType = parameters[0].ParameterType;

                // Hook-up to check whether the collection contains the item when the collection changes
                ((INotifyCollectionChanged)e.NewValue).CollectionChanged += behavior.OnCollectionChanged;

                // Check whether the item is present in the collection
                behavior.checkContains();
            }

            // Unhook from the previous collection
            if (e.OldValue is INotifyCollectionChanged)
            {
                ((INotifyCollectionChanged)e.OldValue).CollectionChanged -= behavior.OnCollectionChanged;
            }
        }