Esempio n. 1
0
        /// <summary>
        /// Get the value to display through reflection on <paramref name="item"/> using property <see cref="DisplayMemberPath"/>
        /// </summary>
        /// <param name="item">Item to get value from.</param>
        /// <returns>Value of the property <see cref="DisplayMemberPath"/> if not <c>null</c>; otherwise ToString()</returns>
        /// <exception cref="ArgumentException">If no property with name <see cref="DisplayMemberPath"/> is found</exception>
        protected virtual string GetDisplayMember(object item)
        {
            if (DisplayMemberFunc != null)
            {
                return(DisplayMemberFunc(item));
            }
            if (item == null)
            {
                return(string.Empty);
            }
            if (IsPrimitive(item) || string.IsNullOrEmpty(DisplayMemberPath))
            {
                return(item.ToString());
            }
            // Find the property by walking the display member path to find any nested properties
            var    propertyPathParts = DisplayMemberPath.Split('.');
            object propertyValue     = item;

            foreach (var propertyPathPart in propertyPathParts)
            {
                var propInfo = propertyValue.GetType().GetTypeInfo().GetDeclaredProperty(propertyPathPart);
                if (propInfo == null)
                {
                    throw new ArgumentException($"No property '{propertyPathPart}' was found on '{propertyValue.GetType().FullName}'");
                }
                propertyValue = propInfo.GetValue(propertyValue);
            }
            if (propertyValue == null)
            {
                throw new ArgumentException($"No property '{DisplayMemberPath}' was found on '{item.GetType().FullName}'");
            }
            return(propertyValue as string);
        }
        private void CheckSelectedPropertyInfo()
        {
            object firstItem = null;

            foreach (var item in ItemsSource)
            {
                firstItem = item;
                break;
            }
            if (firstItem == null)
            {
                return;
            }

            if (DisplayMemberPath.IsNullOrEmpty())
            {
                throw new Exception("Property 'DisplayMemberPath' can not be null or empty.");
            }

            if (SelectedMemberPath.IsNullOrEmpty())
            {
                throw new Exception("Property 'SelectedMemberPath' can not be null.");
            }

            _selectedPropertyInfo = firstItem.GetType().GetProperty(SelectedMemberPath);
            _displayPropertyInfo  = firstItem.GetType().GetProperty(DisplayMemberPath);

            if (_displayPropertyInfo == null)
            {
                throw new Exception("'" + DisplayMemberPath + "' does not existed.");
            }
            if (_selectedPropertyInfo == null || _selectedPropertyInfo.PropertyType != typeof(bool))
            {
                throw new Exception("'" + SelectedMemberPath + "' does not existed , or is not 'bool' type.");
            }
        }