当前位置: 首页 > news >正文

xceed PropertyGrid 如何做成Visual Studio 的属性窗口样子

类似这样的,我百度了一下,发现使用Xceed 不错。使用PropertyGrid

前台代码为

<Windowx:Class="WpfApp.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:local="clr-namespace:WpfApp"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"Title="PropertyGrid Event Example"Width="800"Height="450"d:DataContext="{d:DesignInstance Type=local:MainViewModel}"mc:Ignorable="d"><Window.Resources><!--  定义转换器  --><local:CommandItemToCommandConverter x:Key="CommandItemToCommandConverter" /><!--  定义模板  --><DataTemplate x:Key="EventEditTemplate"><StackPanel Orientation="Horizontal"><!--  下拉框选择命令  --><ComboBoxWidth="150"DisplayMemberPath="Name"ItemsSource="{Binding DataContext.ButtonViewModel.Commands, RelativeSource={RelativeSource AncestorType=Window}}"SelectedItem="{Binding DataContext.ButtonViewModel.EventClick, RelativeSource={RelativeSource AncestorType=Window}, Converter={StaticResource CommandItemToCommandConverter}, Mode=TwoWay}" /></StackPanel></DataTemplate></Window.Resources><StackPanel Orientation="Vertical"><!--  PropertyGrid that binds to the Button object  --><xctk:PropertyGrid Name="MyPropertyGrid" SelectedObject="{Binding ButtonViewModel}"><xctk:PropertyGrid.EditorDefinitions><xctk:EditorTemplateDefinition EditingTemplate="{StaticResource EventEditTemplate}" TargetProperties="EventClick" /></xctk:PropertyGrid.EditorDefinitions></xctk:PropertyGrid><ButtonWidth="100"Height="100"Command="{Binding ButtonViewModel.EventClick, NotifyOnSourceUpdated=True}"Content="sub" /></StackPanel>
</Window>

后台代码为

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Globalization;
using System.IO;
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.Interactivity;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xaml;
using static WpfApp.ButtonViewModel;namespace WpfApp
{public partial class MainWindow : Window{public MainWindow(){InitializeComponent();DataContext = new MainViewModel(); // Set the DataContext to ViewModel}}public class MainViewModel{public ButtonViewModel ButtonViewModel { get; }public MainViewModel(){ButtonViewModel = new ButtonViewModel();}}public class CommandItemToCommandConverter : IValueConverter{public object Convert(object value, Type targetType, object parameter, CultureInfo culture){// 将 ICommand 转换回 CommandItemif (value is ICommand command){var viewModel = Application.Current.MainWindow.DataContext as ButtonViewModel;if (viewModel != null){return viewModel.Commands.FirstOrDefault(item => item.Command == command);}}return null;}public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture){// 从 CommandItem 提取 ICommandif (value is CommandItem commandItem){return commandItem.Command;}return null;}}public class CommandItem{public string Name { get; set; }public ICommand Command { get; set; }}public class ButtonViewModel : INotifyPropertyChanged{private ICommand _eventClick;public ObservableCollection<CommandItem> Commands { get; set; }public ICommand EventClick{get => _eventClick;set{_eventClick = value;OnPropertyChanged(nameof(EventClick));}}public ButtonViewModel(){Commands = new ObservableCollection<CommandItem>{new CommandItem{Name = "Say Hello",Command = new RelayCommand(_ => MessageBox.Show("Hello!"))},new CommandItem{Name = "Say Goodbye",Command = new RelayCommand(_ => MessageBox.Show("Goodbye!"))}};// 初始化默认命令EventClick = Commands.FirstOrDefault()?.Command;}public event PropertyChangedEventHandler PropertyChanged;protected void OnPropertyChanged(string propertyName){PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));}}public class RelayCommand : ICommand{private readonly Action<object> _execute;private readonly Predicate<object> _canExecute;public RelayCommand(Action<object> execute) : this(execute, null){ }public RelayCommand(Action<object> execute, Predicate<object> canExecute){_execute = execute ?? throw new ArgumentNullException(nameof(execute));_canExecute = canExecute;}public bool CanExecute(object parameter){return true;}public void Execute(object parameter){_execute(parameter);}public event EventHandler CanExecuteChanged{add { CommandManager.RequerySuggested += value; }remove { CommandManager.RequerySuggested -= value; }}}
}

这是一个完整可以运行的例子。其实要做成vs 那样的,路途很遥远,这里只是举个例子,需要重写很多模板,像前面的代码中就是定义了Event 选择的模板

 实际运行效果如下:

http://www.lryc.cn/news/529051.html

相关文章:

  • Fork/Join框架_任务分解与并行执行
  • 智能家居监控系统数据收集积压优化
  • 详解python的单例模式
  • momask-codes 部署踩坑笔记
  • H3CNE-31-BFD
  • 蓝桥备赛指南(5)
  • 讯飞智作 AI 配音技术浅析(一)
  • MySQL(高级特性篇) 14 章——MySQL事务日志
  • openRv1126 AI算法部署实战之——TensorFlow TFLite Pytorch ONNX等模型转换实战
  • 【Redis】常见面试题
  • 每日 Java 面试题分享【第 17 天】
  • 「全网最细 + 实战源码案例」设计模式——桥接模式
  • JavaScript 进阶(上)
  • 【编译原理实验二】——自动机实验:NFA转DFA并最小化
  • 深入探讨:服务器如何响应前端请求及后端如何查看前端提交的数据
  • 如何利用Docker和.NET Core实现环境一致性、简化依赖管理、快速部署与扩展,同时提高资源利用率、确保安全性和生态系统支持
  • @Inject @Qualifier @Named
  • 创建 priority_queue - 进阶(内置类型)c++
  • 2. Java-MarkDown文件解析-工具类
  • 动态规划DP 最长上升子序列模型 登山(题目分析+C++完整代码)
  • css-设置元素的溢出行为为可见overflow: visible;
  • 家居EDI:Hom Furniture EDI需求分析
  • 1、开始简单使用rag
  • Linux Samba 低版本漏洞(远程控制)复现与剖析
  • 安卓(android)实现注册界面【Android移动开发基础案例教程(第2版)黑马程序员】
  • 【 AI agents】letta:2024年代理堆栈演进(中英文翻译)
  • Java中 instanceof 的用法(详解)
  • 联想拯救者R720笔记本外接显示屏方法,显示屏是2K屏27英寸
  • 【RocketMQ 存储】- 一文总结 RocketMQ 的存储结构-基础
  • S4 HANA明确税金本币和外币之间转换汇率确定(OBC8)