Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#XCeed通过代码动态创建PropertyGrid_C#_Wpf_Propertygrid_Xceed - Fatal编程技术网

C#XCeed通过代码动态创建PropertyGrid

C#XCeed通过代码动态创建PropertyGrid,c#,wpf,propertygrid,xceed,C#,Wpf,Propertygrid,Xceed,我正在尝试在代码中动态创建属性grid。到目前为止,我可以在XAML credits中创建并自定义一个PropertyGrid: MainWindow.xaml.cs: public MainWindow() { InitializeComponent(); Sample or = new Sample(); pg.SelectedObject = or; pg.ShowAdvancedOptions

我正在尝试在代码中动态创建
属性grid
。到目前为止,我可以在XAML credits中创建并自定义一个
PropertyGrid

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>

这将是等效代码:

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
做了同样的事情,现在我不能向这个combobox添加元素。我怎么能做到呢?谢谢,我想出来了,你帮了大忙,你不知道:)
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);