C#属性显示

news/2024/7/26 11:02:53/文章来源:https://blog.csdn.net/qq_27474555/article/details/137151891

功能:
显示对象的属性,包括可显示属性、可编辑属性、及不可编辑属性。
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.luyixian.cn/news_show_1034565.aspx

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

相关文章

AI 论道|极狐GitLab 客户私享会上海站成功举办

3 月 22 日下午&#xff0c;极狐GitLab 在上海办公室举办了客户私享会&#xff0c;邀请了来自多个行业的多家客户&#xff0c;围绕 AI 提升研发效率的道法术器进行了充分交流。整个交流时长达两个多小时。 极狐GitLab 战略业务与区域发展副总裁何庆出席了此次活动并致开场辞。他…

Transformer模型-softmax的简明介绍

今天介绍transformer模型的softmax softmax的定义和目的&#xff1a; softmax&#xff1a;常用于神经网络的输出层&#xff0c;以将原始的输出值转化为概率分布&#xff0c;从而使得每个类别的概率值在0到1之间&#xff0c;并且所有类别的概率之和为1。这使得Softmax函数特别适…

负载均衡集群

一、集群的基本原理 集群&#xff1a;数据内容是一致的&#xff0c;集群可以被替代 分布式&#xff1a;各司其职&#xff0c;每台服务器存储自己独有的数据&#xff0c;对外作为单点被访问是访问整体的数据&#xff1b; 分布式是不能被替代的&#xff1b;分布式分为MFS、GFS、…

基于springboot+vue+Mysql的教学视频点播系统

开发语言&#xff1a;Java框架&#xff1a;springbootJDK版本&#xff1a;JDK1.8服务器&#xff1a;tomcat7数据库&#xff1a;mysql 5.7&#xff08;一定要5.7版本&#xff09;数据库工具&#xff1a;Navicat11开发软件&#xff1a;eclipse/myeclipse/ideaMaven包&#xff1a;…

设计模式之代理模式解析(上)

代理模式 1&#xff09;概述 1.定义 给某一个对象提供一个代理或占位符&#xff0c;并由代理对象来控制对原对象的访问。 2.作用 当无法直接访问某个对象或访问某个对象存在困难时可以通过一个代理对象来间接访问&#xff0c;为了保证客户端使用的透明性&#xff0c;所访问…

ssm018简易版营业厅宽带系统+jsp

营业厅宽带系统设计与实现 摘 要 现代经济快节奏发展以及不断完善升级的信息化技术&#xff0c;让传统数据信息的管理升级为软件存储&#xff0c;归纳&#xff0c;集中处理数据信息的管理方式。本营业厅宽带系统就是在这样的大环境下诞生&#xff0c;其可以帮助管理者在短时间…

Python快速入门系列-9(Python项目实战)

第九章:Python项目实战 9.1 开发一个简单的Web应用9.1.1 项目概述9.1.2 环境准备9.1.3 项目结构9.1.4 代码实现9.1.4.1 创建数据库模型9.1.4.2 创建视图9.1.4.3 实用工具函数9.1.4.4 运行应用9.1.5 模板设计9.2 数据分析与可视化项目9.2.1 项目概述9.2.2 环境准备9.2.3 数据分…

主干网络篇 | YOLOv8改进之用RCS-OSA替换C2f(来源于RCS-YOLO)

前言:Hello大家好,我是小哥谈。RCS-YOLO是一种目标检测算法,它是基于YOLOv3算法的改进版本。通过查看RCS-YOLO的整体架构可知,其中包括RCS-OSA模块。RCS-OSA模块在模型中用于堆叠RCS模块,以确保特征的复用并加强不同层之间的信息流动。本文就给大家详细介绍如何将RCS-YOLO…

使用Java流API构建树形结构数据

简介&#xff1a; 在实际开发中&#xff0c;构建树状层次结构是常见需求&#xff0c;如组织架构、目录结构或菜单系统。本教案通过解析给定的Java代码&#xff0c;展示如何使用Java 8 Stream API将扁平化的菜单数据转换为具有层级关系的树形结构。 1. 核心类定义 - Menu Data…

Day65-企业级防火墙iptables精讲1

Day65-企业级防火墙iptables精讲1 补充&#xff1a;1.什么是防火墙&#xff1f;2.防火墙种类2.1 商用防火墙介绍2.2 Linux下防火墙介绍 3.选择何种防火墙&#xff1f;4.企业级架构最佳防火墙场景5.学好iptables的技术栈基础6.Iptables是什么&#xff1f;7.Iptables企业常用场景…

Kafka架构概述

Kafka的体系结构 Kafka是由Apache软件基金会管理的一个开源的分布式数据流处理平台。Kafka具有支持消息的发布/订阅模式、高吞吐量与低延迟、持久化、支持水平扩展、高可用性等特点。可以将Kafka应用于大数据实时处理、高性能数据管道、流分析、数据集成和关键任务应用等场景。…

Java入门学习Day04

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

【Python BUG】ModuleNotFoundError: No module named ‘streamlit.cli‘

问题 streamlit做大模型前端demo&#xff0c;安装后不好使。 解决方案 参考&#xff1a; https://zhuanlan.zhihu.com/p/656164361 找到下面文件&#xff1a; 替换、修改内容&#xff1a; # from streamlit.cli import main from streamlit.web.cli import main原来是上边…

【HTML】注册页面制作 案例二

&#xff08;大家好&#xff0c;今天我们将通过案例实战对之前学习过的HTML标签知识进行复习巩固&#xff0c;大家和我一起来吧&#xff0c;加油&#xff01;&#x1f495;&#xff09; 案例复习 通过综合案例&#xff0c;主要复习&#xff1a; 表格标签&#xff0c;可以让内容…

力扣Lc26--- 1108. IP 地址无效化(java版)-2024年4月02日

1.题目描述 2.知识点 注1&#xff1a;首先&#xff0c;在Java中&#xff0c;字符类型应该使用单引号’&#xff0c;而不是双引号"。其次&#xff0c;修改字符数组中的元素应该使用单引号。 注2&#xff1a;String类的replace方法用于在字符串中替换指定的字符或字符序列。…

Java获取IP地址以及MAC地址(附Demo)

目录 前言1. IP及MAC2. 特定适配器 前言 需要获取客户端的IP地址以及MAC地址 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader;public class test {public static void main(String[] args) {try {// 执行命令Process process…

158 Linux C++ 通讯架构实战13,epoll 原理和函数介绍,epoll_create,epoll_ctl ,epoll_wait

epoll技术简介 //&#xff08;2.1&#xff09;epoll概述 //(1)I/O多路复用&#xff1a;epoll就是一种典型的I/O多路复用技术:epoll技术的最大特点是支持高并发&#xff1b; //传统多路复用技术select,poll&#xff0c;在并发量达到1000-2000&#xff0c;性能就会明显下…

python爬取B站视频

参考&#xff1a;https://cloud.tencent.com/developer/article/1768680 参考的代码有点问题&#xff0c;请求头需要修改&#xff0c;上代码&#xff1a; import requests import re # 正则表达式 import pprint import json from moviepy.editor import AudioFileClip, Vid…

【热门话题】WebKit架构简介

&#x1f308;个人主页: 鑫宝Code &#x1f525;热门专栏: 闲话杂谈&#xff5c; 炫酷HTML | JavaScript基础 ​&#x1f4ab;个人格言: "如无必要&#xff0c;勿增实体" 文章目录 WebKit架构简介一、引言二、WebKit概览1. 起源与发展2. 模块化设计 三、WebCore…

LabVIEW挖坑指南

一、挖坑指南 1.1、输出变量放在条件框内 错误写法&#xff1a; 现象&#xff1a;如果没进入对应的分支&#xff0c;输出为默认值 正常写法&#xff1a; 让每个分支输出的值都在预料之内。 1.2、统计耗时不准 错误写法 现象&#xff1a;统计出来的耗时是2000ms 正常写法&a…