最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#屬性顯示的實現(xiàn)示例

 更新時間:2024年04月03日 09:01:30   作者:大浪淘沙胡  
本文主要介紹了C#屬性顯示的實現(xiàn)示例,顯示對象的屬性,包括可顯示屬性、可編輯屬性、及不可編輯屬性,下面就具有來介紹一下,感興趣的可以了解一下

功能:

顯示對象的屬性,包括可顯示屬性、可編輯屬性、及不可編輯屬性。

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、運行結(jié)果

在這里插入圖片描述

到此這篇關(guān)于C#屬性顯示的實現(xiàn)示例的文章就介紹到這了,更多相關(guān)C#屬性顯示內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

  • c# 實現(xiàn)漢諾塔游戲

    c# 實現(xiàn)漢諾塔游戲

    這篇文章主要介紹了c# 實現(xiàn)漢諾塔游戲的示例,幫助大家更好的理解和使用c# 編程語言,感興趣的朋友可以了解下
    2020-11-11
  • C#使用Spire.Doc設(shè)置Word頁眉頁腳的詳細(xì)教程

    C#使用Spire.Doc設(shè)置Word頁眉頁腳的詳細(xì)教程

    在日常的辦公自動化中,Word 文檔的頁眉頁腳經(jīng)常被用來放置公司 LOGO、標(biāo)題、頁碼等信息,本文給大家介紹了如何借助 C# 與強大的文檔處理庫 Spire.Doc,讓我們可以在代碼層面靈活地對頁眉頁腳進行增刪改查,需要的朋友可以參考下
    2026-03-03
  • WPF仿微信實現(xiàn)截圖功能的方法詳解

    WPF仿微信實現(xiàn)截圖功能的方法詳解

    這篇文章主要介紹了如何利用WPF實現(xiàn)截圖功能(仿微信),文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)或工作有一定幫助,需要的可以參考一下
    2022-07-07
  • Ruby創(chuàng)建數(shù)組方法總結(jié)

    Ruby創(chuàng)建數(shù)組方法總結(jié)

    在本篇文章里小編給大家分享了關(guān)于Ruby創(chuàng)建數(shù)組方法的知識點內(nèi)容,對戲有興趣的朋友們學(xué)習(xí)下。
    2019-01-01
  • C#?WPF實現(xiàn)播放音頻文件的示例詳解

    C#?WPF實現(xiàn)播放音頻文件的示例詳解

    這篇文章主要為大家詳細(xì)介紹了利用C#?WPF實現(xiàn)播放音頻文件的相關(guān)知識,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-03-03
  • 詳解C# 利用反射根據(jù)類名創(chuàng)建類的實例對象

    詳解C# 利用反射根據(jù)類名創(chuàng)建類的實例對象

    這篇文章主要介紹了詳解C# 利用反射根據(jù)類名創(chuàng)建類的實例對象,“反射”其實就是利用程序集的元數(shù)據(jù)信息,感興趣的小伙伴們可以參考一下。
    2017-03-03
  • 一個C#開發(fā)者重溫C++的心路歷程

    一個C#開發(fā)者重溫C++的心路歷程

    作為一個C#開發(fā)為什么要重新學(xué)習(xí)C++呢?因為在C#在很多業(yè)務(wù)場景需要調(diào)用一些C++編寫的COM組件,如果不了解C++,那么,很容易。。。注定是要被C++同事忽悠的
    2019-05-05
  • C#多線程開發(fā)實戰(zhàn)記錄之線程基礎(chǔ)

    C#多線程開發(fā)實戰(zhàn)記錄之線程基礎(chǔ)

    線程是一個獨立的運行單元,每個進程內(nèi)部有多個線程,每個線程可以各自同時執(zhí)行指令,每個線程有自己獨立的棧,但是與進程內(nèi)的其他線程共享內(nèi)存,這篇文章主要給大家介紹了關(guān)于C#多線程開發(fā)實戰(zhàn)記錄之線程基礎(chǔ)的相關(guān)資料,需要的朋友可以參考下
    2021-09-09
  • C#?使用SpecFlow創(chuàng)建BDD測試用例的示例代碼

    C#?使用SpecFlow創(chuàng)建BDD測試用例的示例代碼

    這篇文章主要介紹了C#?使用SpecFlow創(chuàng)建BDD測試用例,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-06-06
  • C# 泛型的簡單理解(安全、集合、方法、約束、繼承)分享

    C# 泛型的簡單理解(安全、集合、方法、約束、繼承)分享

    這篇文章介紹了C# 泛型的簡單理解(安全、集合、方法、約束、繼承),有需要的朋友可以參考一下
    2013-10-10

最新評論

绍兴县| 安福县| 望谟县| 通山县| 东阿县| 淅川县| 辽中县| 镶黄旗| 冷水江市| 商城县| 广东省| 广平县| 上栗县| 航空| 翼城县| 健康| 淮南市| 青神县| 辽阳县| 安西县| 宜丰县| 雷州市| 玉环县| 汕头市| 郴州市| 桃源县| 枣阳市| 大英县| 崇州市| 乐山市| 皋兰县| 靖远县| 邯郸县| 莫力| 绥阳县| 凯里市| 息烽县| 宁乡县| 平阳县| 延庆县| 靖西县|