C#属性显示

功能:
显示对象的属性,包括可显示属性、可编辑属性、及不可编辑属性。
1、MainWindow.xaml

<Window x:Class="FlowChart.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:FlowChart"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><DockPanel><StackPanel DockPanel.Dock="Left" Width="300" Margin="0 0 10 0"><StackPanel Margin="0 10 0 10"><TextBlock Text="属性" FontWeight="Bold" Margin="0 0 0 10"/><local:PropertiesView x:Name="_propertiesView" Height="200"/></StackPanel></StackPanel><Border BorderBrush="Black" BorderThickness="1"></Border></DockPanel>
</Window>

2、MainWindow.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;namespace FlowChart
{/// <summary>/// MainWindow.xaml 的交互逻辑/// </summary>public partial class MainWindow : Window{public MainWindow(){InitializeComponent();DataInitialize();}public List<Selection> selections=new List<Selection>();public void DataInitialize(){Selection selection = new Selection();selection.Location=new Point(0,0);selection.Size=new Size(200,200);//selection.Name = "测试";_propertiesView.SelectedObject= selection;}}public class Selection:INotifyPropertyChanged{private Point _location;public Point Location{get { return _location; }set{_location = value;OnPropertyChanged("Location");}}private Size _size;//[Browsable(false)]public Size Size{get { return _size; }set{_size = value;OnPropertyChanged("Size");}}private string _name="Test";public string Name{get { return _name; }//set { _name = value;//    OnPropertyChanged("Name");//}}public override string ToString(){return GetType().Name;}public event PropertyChangedEventHandler PropertyChanged;protected void OnPropertyChanged(string name){if (PropertyChanged != null)PropertyChanged(this, new PropertyChangedEventArgs(name));}}
}

3、PropertiesView.xaml

<UserControl x:Class="FlowChart.PropertiesView"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:FlowChart"mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="300"><UserControl.Resources><ControlTemplate x:Key="validationErrorTemplate"><DockPanel><Image Source="Resources\empty.png" Height="16" Width="16" DockPanel.Dock="Right" Margin="-18 0 0 0"ToolTip="{Binding ElementName=adorner,Path=AdornedElement.(Validation.Errors)[0].ErrorContent}"></Image><AdornedElementPlaceholder x:Name="adorner"/></DockPanel></ControlTemplate><Style x:Key="gridLineStyle" TargetType="Line"><Setter Property="Stroke" Value="Gray" /><Setter Property="Stretch" Value="Fill" /><Setter Property="Grid.ZIndex" Value="1000" /></Style><Style x:Key="gridHorizontalLineStyle" TargetType="Line" BasedOn="{StaticResource gridLineStyle}"><Setter Property="X2" Value="1" /><Setter Property="VerticalAlignment" Value="Bottom" /><Setter Property="Grid.ColumnSpan"Value="{Binding Path=ColumnDefinitions.Count,RelativeSource={RelativeSource AncestorType=Grid}}"/></Style><Style x:Key="gridVerticalLineStyle" TargetType="Line" BasedOn="{StaticResource gridLineStyle}"><Setter Property="Y2" Value="1" /><Setter Property="HorizontalAlignment" Value="Right" /><Setter Property="Grid.RowSpan" Value="{Binding Path=RowDefinitions.Count,RelativeSource={RelativeSource AncestorType=Grid}}"/></Style></UserControl.Resources><Border BorderThickness="1" BorderBrush="Black"><DockPanel x:Name="_panel"><Border x:Name="_label" Width="50" Height="16"><TextBlock Text="Empty" TextAlignment="Center" Foreground="Gray"/></Border><ScrollViewer x:Name="_gridContainer" VerticalScrollBarVisibility="Auto"><Grid x:Name="_grid"><Grid.ColumnDefinitions><ColumnDefinition/><ColumnDefinition/></Grid.ColumnDefinitions><Line Name="_vLine" Grid.Column="0" Grid.RowSpan="1000" Style="{StaticResource gridVerticalLineStyle}"/><GridSplitter Name="_splitter" Grid.RowSpan="1000"  Margin="0,0,-2,0" Width="4" Background="White" Opacity="0.01" Grid.ZIndex="10000"/></Grid></ScrollViewer></DockPanel></Border>
</UserControl>

4、PropertiesView.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;namespace FlowChart
{/// <summary>/// PropertiesView.xaml 的交互逻辑/// </summary>public partial class PropertiesView : UserControl{public PropertiesView(){InitializeComponent();DisplayProperties();}private object _selectedObject;public object SelectedObject{get { return _selectedObject; }set{if (_selectedObject != value){var obj = _selectedObject as INotifyPropertyChanged;if (obj != null)obj.PropertyChanged -= PropertyChanged;_selectedObject = value;DisplayProperties();obj = _selectedObject as INotifyPropertyChanged;if (obj != null)obj.PropertyChanged += PropertyChanged;}}}void PropertyChanged(object sender, PropertyChangedEventArgs e){DisplayProperties();}private void DisplayProperties(){_panel.Children.Clear();ClearGrid();if (SelectedObject != null){int row = 0;foreach (var prop in SelectedObject.GetType().GetProperties().OrderBy(p => p.Name)){var attr = prop.GetCustomAttributes(typeof(BrowsableAttribute), true);if (attr.Length == 0 || (attr[0] as BrowsableAttribute).Browsable){DisplayProperty(prop, row);row++;}}_panel.Children.Add(_gridContainer);}else{_panel.Children.Add(_label);}}private void ClearGrid(){_grid.RowDefinitions.Clear();for (int i = _grid.Children.Count - 1; i >= 0; i--){if (_grid.Children[i] != _vLine && _grid.Children[i] != _splitter)_grid.Children.RemoveAt(i);}}private void DisplayProperty(PropertyInfo prop, int row){var rowDef = new RowDefinition();rowDef.Height = new GridLength(Math.Max(20, this.FontSize * 2));_grid.RowDefinitions.Add(rowDef);var tb = new TextBlock() { Text = prop.Name };tb.Margin = new Thickness(4);Grid.SetColumn(tb, 0);Grid.SetRow(tb, _grid.RowDefinitions.Count - 1);var ed = new TextBox();ed.PreviewKeyDown += new KeyEventHandler(ed_KeyDown);ed.Margin = new Thickness(0, 2, 14, 0);ed.BorderThickness = new Thickness(0);Grid.SetColumn(ed, 1);Grid.SetRow(ed, _grid.RowDefinitions.Count - 1);var line = new Line();line.Style = (Style)Resources["gridHorizontalLineStyle"];Grid.SetRow(line, row);var binding = new Binding(prop.Name);binding.Source = SelectedObject;binding.ValidatesOnExceptions = true;binding.Mode = BindingMode.OneWay;ed.IsEnabled = false;if (prop.CanWrite){ed.IsEnabled = true;var mi = prop.GetSetMethod();if (mi != null && mi.IsPublic)binding.Mode = BindingMode.TwoWay;}ed.SetBinding(TextBox.TextProperty, binding);var template = (ControlTemplate)Resources["validationErrorTemplate"];Validation.SetErrorTemplate(ed, template);_grid.Children.Add(tb);_grid.Children.Add(ed);_grid.Children.Add(line);}void ed_KeyDown(object sender, KeyEventArgs e){var ed = sender as TextBox;if (ed != null){if (e.Key == Key.Enter){ed.GetBindingExpression(TextBox.TextProperty).UpdateSource();e.Handled = true;}else if (e.Key == Key.Escape)ed.GetBindingExpression(TextBox.TextProperty).UpdateTarget();}}}
}

5、运行结果
在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/788904.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Java中的数组及其使用

目录 数组的基本概念 声明数组 创建数组 初始化数组 访问数组元素 数组的遍历 使用for循环 使用for-each循环 多维数组 数组的局限性和替代品 总结 在Java中&#xff0c;数组是存储固定大小的同类型元素的容器。这意味着你可以用一个单一的变量名存储多个项目的集合…

C++(11): 智能指针shared_ptr

1. 概述 shared_ptr智能指针&#xff0c;本质是“离开作用域会自动调整(减小)引用计数&#xff0c;如果引用计数为0&#xff0c;则会调用析构函数”。这样一来&#xff0c;就进化成类似于int、float等的一种会被自动释放的类型。 2. 初始化智能指针 初始化一个智能指针的方式比…

多态的练习

练习1&#xff1a;笔试&面试 题目1&#xff1a;继承成员变量和继承方法的区别 class Base { int count 10; public void display() { System.out.println(this.count); } } class Sub extends Base { int count 20; public void display() …

深入理解ThreadLocal原理

目录 1- 什么是ThreadLocal &#xff1f;2- ThreadLocal的作用&#xff1f;ThreadLocal实现线程间资源隔离ThreadLocal实现线程内资源共享 3- ThreadLocal 原理3-1 ThreadLocalMap3-2 ThreadLocalMap的扩容&#x1f511;1. 为什么会发生扩容&#xff1f;&#x1f511;2. Thread…

Python图像表征空间频率域处理和模式分析

&#x1f3af;要点 Python空间滤波器&#xff1a;&#x1f3af;卷积计算实现均值滤波器。&#x1f3af;非线性中值滤波器。&#x1f3af;最大值/最小值滤波器。&#x1f3af;一阶导数滤波器&#xff1a;索贝尔(sobel)滤波器、普鲁伊特(Prewitt)滤波器、坎尼(Canny)滤波器。&am…

将图像转换为ASCII艺术形式

将图像转换为ASCII艺术形式 在本文中&#xff0c;我们将介绍一个使用OpenCV库将图像转换为ASCII艺术形式的简单程序。ASCII艺术是一种使用字符来表现图像的艺术形式&#xff0c;通过在终端或文本文件中显示字符的不同密度和颜色来模拟图像。这种技术已经存在了几十年&#xff…

【MySQL】7.MHA高可用配置及故障切换

什么是MHA MHA&#xff08;MasterHigh Availability&#xff09;是一套优秀的MySQL高可用环境下故障切换和主从复制的软件 mha用于解决mysql的单点故障问题&#xff1b; 出现故障时&#xff0c;mha能在0~30秒内自动完成故障切换&#xff1b; 并且能在故障切换过程中&#xff0…

史上最强 PyTorch 2.2 GPU 版最新安装教程

一 深度学习主机 1.1 配置 先附上电脑配置图&#xff0c;如下&#xff1a; 利用公司的办公电脑对配置进行升级改造完成。除了显卡和电源&#xff0c;其他硬件都是公司电脑原装。 1.2 显卡 有钱直接上 RTX4090&#xff0c;也不能复用公司的电脑&#xff0c;其他配置跟不上。…

ARM FVP平台的terminal窗口大小如何设置

当启动ARM FVP平台时&#xff0c;terminal窗口太小怎么办&#xff1f;看起来非常累眼睛&#xff0c;本博客来解决这个问题。 首先看下ARM FVP平台对Host主机的需求&#xff1a; 通过上图可知&#xff0c;UART默认使用的是xterm。因此&#xff0c;我们需要修改xterm的默认字体设…

C++语言学习(一)——关键字、命名空间、输入输出

1. C关键字 C总计63个关键字&#xff0c;C语言32个关键字 2. 命名空间 在C/C中&#xff0c;变量、函数和后面要学到的类都是大量存在的&#xff0c;这些变量、函数和类的名称将都存在于全局作用域中&#xff0c;可能会导致很多冲突。使用命名空间的目的是对标识符的名称进行本…

vue3从精通到入门9:计算属性computed

在 Vue 3 中&#xff0c;computed 是一个用于创建计算属性的工具&#xff0c;它基于组件的响应式依赖进行复杂的计算&#xff0c;并返回一个新的响应式引用。计算属性是 Vue 的一个核心概念&#xff0c;它提供了一种声明式的方式来执行基于其依赖的响应式数据的计算。 compute…

详细总结前中后序、层次遍历二叉树(非递归方法)

二叉树 结构 //二叉树节点结构 class Node<V>{V value;Node left;Node right;}之前一直学的是用递归方法进行前中后序三种遍历方法&#xff0c;没想到用非递归方法也还是挺舒服的&#xff0c;对了解树结构的应用也很有帮助 &#xff08;主要用到的思路就是借助栈或者队列…

yolov5关键点检测-实现溺水检测与警报提示(代码+原理)

基于YOLOv5的关键点检测应用于溺水检测与警报提示是一种结合深度学习与计算机视觉技术的安全监控解决方案。该项目通常会利用YOLOv5强大的实时目标检测能力&#xff0c;并通过扩展或修改网络结构以支持人体关键点检测&#xff0c;来识别游泳池或其他水域中人们的行为姿态。 项…

【机器学习】《机器学习算法竞赛实战》思考练习(更新中……)

文章目录 第2章 问题建模&#xff08;一&#xff09;对于多分类问题&#xff0c;可否将其看作回归问题进行处理&#xff0c;对类别标签又有什么要求&#xff1f;&#xff08;二&#xff09;目前给出的都是已有的评价指标&#xff0c;那么这些评价指标&#xff08;分类指标和回归…

Java入门学习Day04

本篇文章主要介绍了&#xff1a;如何输入数据、字符串拼接、自增自减运算符、类型转换&#xff08;int&#xff0c;double等&#xff09; CSDN&#xff1a;码银 公众号&#xff1a;码银学编程 一、键盘输入练习 Scanner是Java中的一个类&#xff0c;用于从控制台或文件中读…

DOTS:Burst

目录 一&#xff1a;简介 1.1 Getting started 1.2 C# language support 1.2.1 HPC# overview 1.2.1.1 Exception expressions 1.2.1.2 Foreach and While 1.2.1.3 Unsupported C# features in HPC# 1.2.2 Static read-only fields and static constructor support 1.…

STM32-03基于HAL库(CubeMX+MDK+Proteus)输入检测案例(按键控制LED)

文章目录 一、功能需求分析二、Proteus绘制电路原理图三、STMCubeMX 配置引脚及模式&#xff0c;生成代码四、MDK打开生成项目&#xff0c;编写HAL库的按键检测代码五、运行仿真程序&#xff0c;调试代码 一、功能需求分析 搭建完成开发STM32开发环境之后&#xff0c;开始GPIO…

LC 110.平衡二叉树

110. 平衡二叉树 给定一个二叉树&#xff0c;判断它是否是高度平衡的二叉树。 本题中&#xff0c;一棵高度平衡二叉树定义为&#xff1a; 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1 。 示例 1&#xff1a; 输入&#xff1a; root [3,9,20,null,null,15,7]…

【python】python 测试,为什么我们需要测试,pytest的使用

为什么需要测试 &#x1f525;测试&#xff0c;软件开发的秘密武器&#x1f525; ✨大家好&#xff0c;今天就来跟大家聊聊软件开发中的一项超级重要的环节——测试&#xff01;&#x1f440; &#x1f3af;测试&#xff0c;是软件开发的灵魂&#xff0c;是确保代码正确运行…

MySQL中innodb_status_output_locks含义和用法

innodb_status_output_locks 是一个MySQL系统变量&#xff0c;它决定了是否在 SHOW ENGINE INNODB STATUS 输出中包含详细的锁定信息。其主要用途是帮助分析和诊断InnoDB存储引擎中的锁定问题&#xff0c;比如事务死锁或长时间持有的锁导致的性能问题。 当innodb_status_outpu…