C#属性显示的实现示例

 更新时间:2024年04月03日 09:01:30   作者:大浪淘沙胡  
本文主要介绍了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、运行结果

在这里插入图片描述

到此这篇关于C#属性显示的实现示例的文章就介绍到这了,更多相关C#属性显示内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家! 

相关文章

  • c# 实现汉诺塔游戏

    c# 实现汉诺塔游戏

    这篇文章主要介绍了c# 实现汉诺塔游戏的示例,帮助大家更好的理解和使用c# 编程语言,感兴趣的朋友可以了解下
    2020-11-11
  • C#使用Spire.Doc设置Word页眉页脚的详细教程

    C#使用Spire.Doc设置Word页眉页脚的详细教程

    在日常的办公自动化中,Word 文档的页眉页脚经常被用来放置公司 LOGO、标题、页码等信息,本文给大家介绍了如何借助 C# 与强大的文档处理库 Spire.Doc,让我们可以在代码层面灵活地对页眉页脚进行增删改查,需要的朋友可以参考下
    2026-03-03
  • WPF仿微信实现截图功能的方法详解

    WPF仿微信实现截图功能的方法详解

    这篇文章主要介绍了如何利用WPF实现截图功能(仿微信),文中的示例代码讲解详细,对我们学习或工作有一定帮助,需要的可以参考一下
    2022-07-07
  • Ruby创建数组方法总结

    Ruby创建数组方法总结

    在本篇文章里小编给大家分享了关于Ruby创建数组方法的知识点内容,对戏有兴趣的朋友们学习下。
    2019-01-01
  • C# WPF实现播放音频文件的示例详解

    C# WPF实现播放音频文件的示例详解

    这篇文章主要为大家详细介绍了利用C# WPF实现播放音频文件的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2024-03-03
  • 详解C# 利用反射根据类名创建类的实例对象

    详解C# 利用反射根据类名创建类的实例对象

    这篇文章主要介绍了详解C# 利用反射根据类名创建类的实例对象,“反射”其实就是利用程序集的元数据信息,感兴趣的小伙伴们可以参考一下。
    2017-03-03
  • 一个C#开发者重温C++的心路历程

    一个C#开发者重温C++的心路历程

    作为一个C#开发为什么要重新学习C++呢?因为在C#在很多业务场景需要调用一些C++编写的COM组件,如果不了解C++,那么,很容易。。。注定是要被C++同事忽悠的
    2019-05-05
  • C#多线程开发实战记录之线程基础

    C#多线程开发实战记录之线程基础

    线程是一个独立的运行单元,每个进程内部有多个线程,每个线程可以各自同时执行指令,每个线程有自己独立的栈,但是与进程内的其他线程共享内存,这篇文章主要给大家介绍了关于C#多线程开发实战记录之线程基础的相关资料,需要的朋友可以参考下
    2021-09-09
  • C# 使用SpecFlow创建BDD测试用例的示例代码

    C# 使用SpecFlow创建BDD测试用例的示例代码

    这篇文章主要介绍了C# 使用SpecFlow创建BDD测试用例,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-06-06
  • C# 泛型的简单理解(安全、集合、方法、约束、继承)分享

    C# 泛型的简单理解(安全、集合、方法、约束、继承)分享

    这篇文章介绍了C# 泛型的简单理解(安全、集合、方法、约束、继承),有需要的朋友可以参考一下
    2013-10-10

最新评论