Exemplo n.º 1
0
        public ControlDetailsViewModel(TreePageViewModel treePage, IVisual control)
        {
            _control = control;

            TreePage = treePage;

            var properties = GetAvaloniaProperties(control)
                             .Concat(GetClrProperties(control))
                             .OrderBy(x => x, PropertyComparer.Instance)
                             .ThenBy(x => x.Name)
                             .ToList();

            _propertyIndex = properties.GroupBy(x => x.Key).ToDictionary(x => x.Key, x => x.ToList());

            var view = new DataGridCollectionView(properties);

            view.GroupDescriptions.Add(new DataGridPathGroupDescription(nameof(AvaloniaPropertyViewModel.Group)));
            view.Filter    = FilterProperty;
            PropertiesView = view;

            Layout = new ControlLayoutViewModel(control);

            if (control is INotifyPropertyChanged inpc)
            {
                inpc.PropertyChanged += ControlPropertyChanged;
            }

            if (control is AvaloniaObject ao)
            {
                ao.PropertyChanged += ControlPropertyChanged;
            }
        }
        public ControlDetailsViewModel(TreePageViewModel treePage, IVisual control)
        {
            _control = control;

            TreePage = treePage;

            var properties = GetAvaloniaProperties(control)
                             .Concat(GetClrProperties(control))
                             .OrderBy(x => x, PropertyComparer.Instance)
                             .ThenBy(x => x.Name)
                             .ToList();

            _propertyIndex = properties.GroupBy(x => x.Key).ToDictionary(x => x.Key, x => x.ToList());

            var view = new DataGridCollectionView(properties);

            view.GroupDescriptions.Add(new DataGridPathGroupDescription(nameof(AvaloniaPropertyViewModel.Group)));
            view.Filter    = FilterProperty;
            PropertiesView = view;

            Layout = new ControlLayoutViewModel(control);

            if (control is INotifyPropertyChanged inpc)
            {
                inpc.PropertyChanged += ControlPropertyChanged;
            }

            if (control is AvaloniaObject ao)
            {
                ao.PropertyChanged += ControlPropertyChanged;
            }

            AppliedStyles = new ObservableCollection <StyleViewModel>();
            PseudoClasses = new ObservableCollection <PseudoClassViewModel>();

            if (control is StyledElement styledElement)
            {
                styledElement.Classes.CollectionChanged += OnClassesChanged;

                var pseudoClassAttributes = styledElement.GetType().GetCustomAttributes <PseudoClassesAttribute>(true);

                foreach (var classAttribute in pseudoClassAttributes)
                {
                    foreach (var className in classAttribute.PseudoClasses)
                    {
                        PseudoClasses.Add(new PseudoClassViewModel(className, styledElement));
                    }
                }

                var styleDiagnostics = styledElement.GetStyleDiagnostics();

                foreach (var appliedStyle in styleDiagnostics.AppliedStyles)
                {
                    var styleSource = appliedStyle.Source;

                    var setters = new List <SetterViewModel>();

                    if (styleSource is Style style)
                    {
                        foreach (var setter in style.Setters)
                        {
                            if (setter is Setter regularSetter)
                            {
                                var setterValue = regularSetter.Value;

                                var resourceInfo = GetResourceInfo(setterValue);

                                SetterViewModel setterVm;

                                if (resourceInfo.HasValue)
                                {
                                    var resourceKey   = resourceInfo.Value.resourceKey;
                                    var resourceValue = styledElement.FindResource(resourceKey);

                                    setterVm = new ResourceSetterViewModel(regularSetter.Property, resourceKey, resourceValue, resourceInfo.Value.isDynamic);
                                }
                                else
                                {
                                    setterVm = new SetterViewModel(regularSetter.Property, setterValue);
                                }

                                setters.Add(setterVm);
                            }
                        }

                        AppliedStyles.Add(new StyleViewModel(appliedStyle, style.Selector?.ToString() ?? "No selector", setters));
                    }
                }

                UpdateStyles();
            }
        }