コード例 #1
0
        /// <summary>
        ///     Watches the specified source.
        /// </summary>
        /// <param name="source">
        ///     The source.
        /// </param>
        /// <param name="property">
        ///     The property.
        /// </param>
        /// <returns>
        ///     The <see cref="WeakCommandBinding"/>.
        /// </returns>
        /// <exception cref="System.ArgumentNullException">
        ///     The source can not be null.
        /// </exception>
        public WeakCommandBinding Watch(object source, string property)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }

            var bindContext = new BindContext(source, property);

            var       entry = new WeakEntry(null, bindContext.Source, property);
            WeakEvent watchEvent;

            if (this.watchEvents.ContainsKey(entry))
            {
                watchEvent = this.watchEvents[entry];
            }
            else
            {
                watchEvent = new WeakEvent(this);
                this.watchEvents.Add(entry, watchEvent);
            }

            string propertyName = property.Contains(".") ? property.Right(".") : property;

            watchEvent.AttachEvent(
                bindContext,
                WeakPropertyBinding.PropertyChangedEventName,
                WatchEventCallbackMethod,
                (o, args) => string.Equals(((PropertyChangedEventArgs)args).PropertyName, propertyName));

            this.RaiseCanExecuteChanged();

            return(this);
        }
コード例 #2
0
        public void Test_WeakEntry_Equals()
        {
            var viewModel = new TestViewModel();
            var viewModel2 = new TestViewModel();

            var weakEntry = new WeakEntry("Text1", viewModel, "Name");
            var weakEntry1 = new WeakEntry("Text1", viewModel2, "Name");
            var weakEntry2 = new WeakEntry("Text2", viewModel, "Name");
            var weakEntry3 = new WeakEntry("Text1", viewModel, "Age");
            var weakEntry4 = new WeakEntry("Text1", viewModel, "Name");
            var weakEntry5 = new WeakEntry(string.Empty, viewModel, "Name");
            var weakEntry6 = new WeakEntry("Text2", viewModel, string.Empty);

            Assert.IsFalse(weakEntry.Equals(weakEntry1));
            Assert.IsFalse(weakEntry.Equals(weakEntry2));
            Assert.IsFalse(weakEntry.Equals(weakEntry3));
            Assert.IsFalse(weakEntry1.Equals(weakEntry2));
            Assert.IsFalse(weakEntry1.Equals(weakEntry3));
            Assert.IsFalse(weakEntry2.Equals(weakEntry5));
            Assert.IsFalse(weakEntry2.Equals(weakEntry6));
            Assert.IsTrue(weakEntry1 != weakEntry3);

            Assert.IsTrue(weakEntry.Equals(weakEntry4));
            Assert.IsTrue(weakEntry == weakEntry4);
        }
コード例 #3
0
        /// <summary>
        ///     Sets the binding.
        /// </summary>
        /// <typeparam name="T">
        ///     The concrete <see cref="WeakBinding"/> to return.
        /// </typeparam>
        /// <param name="targetProperty">
        ///     The target property.
        /// </param>
        /// <param name="source">
        ///     The source.
        /// </param>
        /// <param name="sourceProperty">
        ///     The source property.
        /// </param>
        /// <param name="activate">
        ///     if set to <c>true</c> activate.
        /// </param>
        /// <returns>
        ///     The <see cref="T"/>.
        /// </returns>
        /// <exception cref="System.NotSupportedException">
        ///     The new binding is not compatible with the existing binding.
        /// </exception>
        public T SetBinding <T>(string targetProperty, object source, string sourceProperty, bool activate = true)
            where T : WeakBinding
        {
            var         entry = new WeakEntry(targetProperty, source, sourceProperty);
            WeakBinding binding;

            if (this.bindings.ContainsKey(entry))
            {
                binding = this.bindings[entry];
                if (typeof(T) != binding.GetType())
                {
                    throw new NotSupportedException(
                              string.Format(
                                  "The new {0} is not compatible with the existing {1}, ",
                                  typeof(T),
                                  binding.GetType())
                              + "please clear the binding and rebind or update WeakTarget.SetBinding method to clear the existing binding first");
                }

                binding.Update(targetProperty, source, sourceProperty);
            }
            else
            {
                binding = DynamicEngine.CreateInstance <T>(this.Target, targetProperty, source, sourceProperty);
                binding.Initialize <T>(activate);

                this.bindings.Add(entry, binding);
            }

            return(binding as T);
        }
コード例 #4
0
        /// <summary>
        ///     Clears the binding.
        /// </summary>
        /// <param name="targetProperty">
        ///     The target property.
        /// </param>
        /// <param name="source">
        ///     The source.
        /// </param>
        /// <param name="sourceProperty">
        ///     The source property.
        /// </param>
        public void ClearBinding(string targetProperty, object source, string sourceProperty)
        {
            var entry = new WeakEntry(targetProperty, source, sourceProperty);

            if (this.bindings.ContainsKey(entry))
            {
                this.bindings[entry].Clear();
                this.bindings.Remove(entry);
            }
        }
コード例 #5
0
        /// <summary>
        ///     Get the binding.
        /// </summary>
        /// <typeparam name="T">
        ///     The concrete <see cref="WeakBinding"/> to return.
        /// </typeparam>
        /// <param name="targetProperty">
        ///     The target property.
        /// </param>
        /// <param name="source">
        ///     The source.
        /// </param>
        /// <param name="sourceProperty">
        ///     The source property.
        /// </param>
        /// <returns>
        ///     The <see cref="T"/>.
        /// </returns>
        public T GetBinding <T>(string targetProperty, object source, string sourceProperty)
            where T : WeakBinding
        {
            var entry = new WeakEntry(targetProperty, source, sourceProperty);

            if (this.bindings.ContainsKey(entry))
            {
                return(this.bindings[entry] as T);
            }

            return(null);
        }
コード例 #6
0
        /// <summary>
        ///     UnWatch the source property.
        /// </summary>
        /// <param name="source">
        ///     The source.
        /// </param>
        /// <param name="property">
        ///     The property.
        /// </param>
        /// <returns>
        ///     The <see cref="WeakCommandBinding"/>.
        /// </returns>
        /// <exception cref="System.ArgumentNullException">
        ///     The source can not be null.
        /// </exception>
        public WeakCommandBinding UnWatch(object source, string property)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }

            var bindSource = new BindContext(source, property);

            var entry = new WeakEntry(null, bindSource.Source, property);

            if (this.watchEvents.ContainsKey(entry))
            {
                WeakEvent watchEvent = this.watchEvents[entry];
                watchEvent.DetachEvent();
                this.watchEvents.Remove(entry);
            }

            this.RaiseCanExecuteChanged();

            return(this);
        }