wpf 根据属性,动态创建TextBlock,显示名称和值

private static void OnItemsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is not ExaminationProtocolsControl self)
            {
                return;
            }

            var baseModeOption = (BaseModeOption) e.NewValue;
            self._ParameterPanel_.Children.Clear();
            if (baseModeOption != null && !string.IsNullOrEmpty(baseModeOption.Name))
            {
                System.Reflection.PropertyInfo[] properties;
                Type type = baseModeOption.GetType();
                properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
                object obj = baseModeOption;
                if (properties.Length <= 0)
                {
                    return;
                }

                List list = new List();
                for (int i = 0; i < properties.Length; i += 2)
                {
                    list.Clear();
                    for (int j = 0; j < 2; j++)
                    {
                        if (i + 1 < properties.Length)
                        {
                            string name = "";
                            //得到 Description的值
                            var attributes = properties[i + j].GetCustomAttributes(typeof(DescriptionAttribute), false);
                            if (attributes.Any())
                            {
                                var descriptionAttribute = (DescriptionAttribute) attributes.First();
                                name = descriptionAttribute.Description;
                            }
                            //如果Description不存在,直接得name
                            if (string.IsNullOrEmpty(name))
                            {
                                name = properties[i + j].Name;
                            }
                            //得到属性的value值
                            string value = properties[i + j].GetValue(obj).ToString();
                            list.Add(name);
                            list.Add(value);
                        }
                    }
                    self._ParameterPanel_.Children.Add(GetParameterView(list));
                }
            }
        }

        /// 
        /// 根据list的数量,动态添加 TextBlock,显示属性和值
        /// 
        /// 
        /// 
        private static Border GetParameterView(List list)
        {
            Border border = new Border();
            border.Height = 36;
            StackPanel stackPanel = new StackPanel
            {
                Margin = new Thickness(0, 0, 0, 0),
                Orientation = Orientation.Horizontal
            };
            for (int i = 0; i < list.Count; i++)
            {
                TextBlock textBlock = new TextBlock();
                textBlock.Width = 130;
                textBlock.Margin = new Thickness(5, 0, 5, 0);
                //首字母大写
                string txtValue = char.ToUpper(list[i][0]) + list[i].Substring(1);
                textBlock.Foreground = Brushes.White;
                textBlock.Text = txtValue;
                stackPanel.Children.Add(textBlock);
            }
            border.Child = stackPanel;
            return border;
        }

你可能感兴趣的:(wpf,windows)