Ejemplo n.º 1
0
        protected async System.Threading.Tasks.Task SelectAll()
        {
            if (Disabled)
            {
                return;
            }

            if (selectedItems.Count != View.Cast <object>().Count())
            {
                selectedItems.Clear();
                selectedItems = View.Cast <object>().ToList();
            }
            else
            {
                selectedItems.Clear();
            }

            if (!string.IsNullOrEmpty(ValueProperty))
            {
                System.Reflection.PropertyInfo pi = PropertyAccess.GetElementType(Data.GetType()).GetProperty(ValueProperty);
                Value = selectedItems.Select(i => PropertyAccess.GetItemOrValueFromProperty(i, ValueProperty)).AsQueryable().Cast(pi.PropertyType);
            }
            else
            {
                var type = typeof(T).IsGenericType ? typeof(T).GetGenericArguments()[0] : typeof(T);
                Value = selectedItems.AsQueryable().Cast(type);
            }

            await ValueChanged.InvokeAsync((T)Value);

            if (FieldIdentifier.FieldName != null)
            {
                EditContext?.NotifyFieldChanged(FieldIdentifier);
            }
            await Change.InvokeAsync(Value);

            StateHasChanged();
        }
Ejemplo n.º 2
0
        protected async System.Threading.Tasks.Task SelectItem(object item, bool raiseChange = true)
        {
            if (!Multiple)
            {
                if (object.Equals(item, selectedItem))
                {
                    return;
                }

                selectedItem = item;
                if (!string.IsNullOrEmpty(ValueProperty))
                {
                    Value = PropertyAccess.GetItemOrValueFromProperty(item, ValueProperty);
                }
                else
                {
                    Value = item;
                }

                SetSelectedIndexFromSelectedItem();

                SelectedItemChanged?.Invoke(selectedItem);
            }
            else
            {
                if (selectedItems.IndexOf(item) == -1)
                {
                    selectedItems.Add(item);
                }
                else
                {
                    selectedItems.Remove(item);
                }

                if (!string.IsNullOrEmpty(ValueProperty))
                {
                    System.Reflection.PropertyInfo pi = PropertyAccess.GetElementType(Data.GetType()).GetProperty(ValueProperty);
                    Value = selectedItems.Select(i => PropertyAccess.GetItemOrValueFromProperty(i, ValueProperty)).AsQueryable().Cast(pi.PropertyType);
                }
                else
                {
                    var firstElement = Data.Cast <object>().FirstOrDefault();
                    var elementType  = firstElement != null?firstElement.GetType() : null;

                    if (elementType != null)
                    {
                        Value = selectedItems.AsQueryable().Cast(elementType);
                    }
                    else
                    {
                        Value = selectedItems;
                    }
                }
            }
            if (raiseChange)
            {
                await ValueChanged.InvokeAsync(object.Equals(Value, null)?default(T) : (T)Value);

                if (FieldIdentifier.FieldName != null)
                {
                    EditContext?.NotifyFieldChanged(FieldIdentifier);
                }
                await Change.InvokeAsync(Value);
            }
            StateHasChanged();
        }