【问题标题】:C# XCeed creating PropertyGrid dynamically by codeC# XCeed 通过代码动态创建 PropertyGrid
【发布时间】:2018-05-22 08:54:27
【问题描述】:

我正在尝试在代码中动态创建PropertyGrid。到目前为止,我可以在 XAML 学分中创建和自定义 PropertyGridXCeed PropertyGrid customizing IntegerUpDown :
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);
        }

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>

和 Sample 类:

public class Sample
    {
        private int _Value;

        #region Public Properties

        [Category("Sample")]
        [DisplayName("Sample Value")]
        [DefaultValue(3)]
        public int Value { set; get; }

       #endregion

    }

【问题讨论】:

    标签: c# wpf propertygrid xceed


    【解决方案1】:

    这将是等效的代码:

    EditorDefinition ed = new EditorDefinition();
    PropertyDefinition pd = new PropertyDefinition();
    pd.Name = "Value";
    ed.PropertiesDefinitions.Add(pd);
    
    FrameworkElementFactory fac = new FrameworkElementFactory(typeof(PropertyGridEditorIntegerUpDown));
    fac.SetBinding(PropertyGridEditorIntegerUpDown.ValueProperty, new Binding("Value"));
    fac.SetValue(PropertyGridEditorIntegerUpDown.IncrementProperty, 10);
    
    DataTemplate dt = new DataTemplate { VisualTree = fac };
    dt.Seal();
    ed.EditorTemplate = dt;
    pg.EditorDefinitions.Add(ed);
    

    【讨论】:

    • 再次感谢 :) 我现在有另一个问题,我为 PropertyGridEditorComboBox 做了同样的事情,现在我不能向这个组合框添加元素。我该怎么做?
    • 谢谢,我想通了,你帮了大忙,你不知道:)
    猜你喜欢
    • 1970-01-01
    • 2012-02-05
    • 1970-01-01
    • 1970-01-01
    • 2018-10-30
    • 1970-01-01
    • 2012-04-30
    • 2021-11-08
    • 2022-10-15
    相关资源
    最近更新 更多