예제 #1
0
    /// <summary>
    /// Returns the template element with specified name and type, or throws an exception if not found.
    /// </summary>
    /// <param name="nameScope">The NameScope used to find the element.</param>
    /// <param name="name">The name of the element to find.</param>
    /// <typeparam name="T">The type of the element to find.</typeparam>
    /// <returns>The element found.</returns>
    /// <exception cref="InvalidCastException">The element is not found.</exception>
    public static T FindOrThrow <T>(this INameScope nameScope, string name)
        where T : class
    {
        var part = nameScope.Find <T>(name);

        if (part == null)
        {
            throw new InvalidCastException(string.Format(CultureInfo.InvariantCulture,
                                                         Properties.Resources.TemplateElementNotFound, name, nameof(T)));
        }
        return(part);
    }
예제 #2
0
        public object Find(string name)
        {
            var found = _inner.Find(name);

            if (found != null)
            {
                return(found);
            }
            if (_inner.IsCompleted)
            {
                return(_parentScope.Find(name));
            }
            return(null);
        }
예제 #3
0
        /// <summary>
        /// Finds a named element in an <see cref="INameScope"/>.
        /// </summary>
        /// <typeparam name="T">The element type.</typeparam>
        /// <param name="nameScope">The name scope.</param>
        /// <param name="name">The name.</param>
        /// <returns>The named element or null if not found.</returns>
        public static T Find <T>(this INameScope nameScope, string name)
            where T : class
        {
            Contract.Requires <ArgumentNullException>(nameScope != null);
            Contract.Requires <ArgumentNullException>(name != null);

            var result = nameScope.Find(name);

            if (result != null && !(result is T))
            {
                throw new InvalidOperationException(
                          $"Expected control '{name}' to be '{typeof(T)} but it was '{result.GetType()}'.");
            }

            return((T)result);
        }
예제 #4
0
        /// <summary>
        /// Gets a named element from an <see cref="INameScope"/> or throws if no element of the
        /// requested name was found.
        /// </summary>
        /// <typeparam name="T">The element type.</typeparam>
        /// <param name="nameScope">The name scope.</param>
        /// <param name="name">The name.</param>
        /// <returns>The named element.</returns>
        public static T Get <T>(this INameScope nameScope, string name)
            where T : class
        {
            _ = nameScope ?? throw new ArgumentNullException(nameof(nameScope));
            _ = name ?? throw new ArgumentNullException(nameof(name));

            var result = nameScope.Find(name);

            if (result == null)
            {
                throw new KeyNotFoundException($"Could not find control '{name}'.");
            }

            if (!(result is T))
            {
                throw new InvalidOperationException(
                          $"Expected control '{name}' to be '{typeof(T)} but it was '{result.GetType()}'.");
            }

            return((T)result);
        }
예제 #5
0
            private void Update()
            {
                if (_name != null)
                {
                    _nameScope = _relativeTo.FindNameScope();

                    if (_nameScope != null)
                    {
                        _nameScope.Registered   += Registered;
                        _nameScope.Unregistered += Unregistered;
                        _value = _nameScope.Find <ILogical>(_name);
                    }
                    else
                    {
                        _value = null;
                    }
                }
                else
                {
                    _value = _relativeTo.GetLogicalAncestors()
                             .Where(x => _ancestorType?.GetTypeInfo().IsAssignableFrom(x.GetType().GetTypeInfo()) ?? true)
                             .ElementAtOrDefault(_ancestorLevel);
                }
            }
예제 #6
0
 /// <inheritdoc/>
 protected override void OnTemplateApplied(INameScope nameScope)
 {
     Presenter = nameScope.Find <IItemsPresenter>("PART_ItemsPresenter");
 }
예제 #7
0
 /// <inheritdoc/>
 protected override void OnTemplateApplied(INameScope nameScope)
 {
     Presenter = nameScope.Find<IItemsPresenter>("PART_ItemsPresenter");
 }
예제 #8
0
 /// <inheritdoc/>
 protected override void OnTemplateApplied(INameScope nameScope)
 {
     // We allow ContentControls without ContentPresenters in the template. This can be
     // useful for e.g. a simple ToggleButton that displays an image. There's no need to
     // have a ContentPresenter in the visual tree for that.
     Presenter = nameScope.Find<ContentPresenter>("PART_ContentPresenter");
 }