You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过代码动态创建并自定义XCeed PropertyGrid控件?

我正在尝试通过代码动态创建Xceed PropertyGrid控件,目前已经能在XAML中完成创建和自定义,参考了XCeed PropertyGrid自定义PropertyGridEditorIntegerUpDown的实现方式,以下是我的代码:

XAML实现(MainWindow.xaml)

<Window 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:WpfApp1" 
        xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit" 
        xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid" 
        x:Class="WpfApp1.MainWindow" 
        mc:Ignorable="d" 
        Title="MainWindow" Height="450" Width="800">
    <xctk:PropertyGrid x:Name="pg">
        <xctk:PropertyGrid.EditorDefinitions >
            <xctk:EditorDefinition >
                <xctk:EditorDefinition.PropertiesDefinitions >
                    <xctk:PropertyDefinition Name = "Value" />
                </xctk:EditorDefinition.PropertiesDefinitions >
                <xctk:EditorDefinition.EditorTemplate >
                    <DataTemplate >
                        <xctk:PropertyGridEditorIntegerUpDown Increment = "10" Value = "{Binding Value}" Maximum = "40" MinHeight = "0" Minimum = "-30" />
                    </DataTemplate >
                </xctk:EditorDefinition.EditorTemplate >
            </xctk:EditorDefinition >
        </xctk:PropertyGrid.EditorDefinitions >
    </xctk:PropertyGrid>
</Window>

后台代码实现(MainWindow.xaml.cs)

public MainWindow()
{
    InitializeComponent();
    Sample or = new Sample();
    pg.SelectedObject = or;
    pg.ShowAdvancedOptions = true;
    EditorDefinition ed = new EditorDefinition();
    PropertyDefinition pd = new PropertyDefinition();
    pd.Name = "Value";
    ed.PropertyDefinitions.Add(pd);
    DataTemplate dt = new DataTemplate();
    FrameworkElementFactory fac = new FrameworkElementFactory(typeof(PropertyGridEditorIntegerUpDown));
    dt.VisualTree = fac;
    DependencyProperty dp = PropertyGridEditorIntegerUpDown.DefaultValueProperty;
    fac.SetValue(dp, 10);
    ed.EditorTemplate = dt;
    pg.EditorDefinitions.Add(ed);
}

Sample数据类

public class Sample
{
    private int _Value;
    #region Public Properties
    [Category("Sample")]
    [DisplayName("Sample Value")]
    [DefaultValue(3)]
    public int Value { set; get; }
    #endregion
}

内容的提问来源于stack exchange,提问作者hebele

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:21:55