Not able to add value in combo box Custom editor

Grids for WPF Forum

Posted 15 years ago by Anurodh Ora
Version: 4.5.0486
Avatar
Hi,

#1

I am adding Custom editor in the property grid in the following manner.
The values which i have to display in combo are in the ITEM.Value (can change the format)

But i am not getting that how to set values in the editor.

Data template in resource dictionary


<DataTemplate x:Key="test">
<Grid>
<TextBox Grid.Column="0" Margin="0" HorizontalAlignment="Stretch" VerticalAlignment="Center" BorderThickness="0"
Text="{Binding Value, RelativeSource={RelativeSource AncestorType={x:Type propgrid:IPropertyDataAccessor}}, Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True}"
IsReadOnly="{Binding IsReadOnly, RelativeSource={RelativeSource AncestorType={x:Type propgrid:IPropertyDataAccessor}}}" />        
</Grid>
</DataTemplate>


PropertyGridPropertyItem item = new PropertyGridPropertyItem();
item.DisplayName = "MyComboBox";
item.Value = "1,2,3";
item.Description = "This is sample combobox property";
item.Category = "My Properties:";
            
// Here I have to create FrameworkElementFactory to make this element value type ComboBox.

FrameworkElementFactory fact = new FrameworkElementFactory(typeof(ComboBox), 
"ctlCombo");

DataTemplate dataTemplate = new DataTemplate();
dataTemplate.Resources.Add(typeof(DataTemplate), this.FindResource("test"));
dataTemplate.VisualTree = fact;
item.ValueTemplate = dataTemplate;

// Finally Add it to Properties Collection.

propGrid.Properties.Add(item);

// How can I add items on ComboBox which is created above?

#2

Now suppose, I have Combo box as custom editor with 5 values and displaying in property grid. Now on any event How can I change value of combo which is at other than first index.


if you have sample code for above issue please send me, its very urgent.

Thanks

Anurodh

Comments (2)

Posted 15 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Anurodh,

Your code doesn't make sense. You are loading one DataTemplate (from XAML resource) into another DataTemplate (from code-behind). You should do one or the other, but not both.

If you want to use the DataTemplate from the XAML resource, then you would assign the item.ValueTemplate to the result of "(DataTemplate)this.FindResource("test")". This DataTemplate should then have a ComboBox control in it, not a TextBox. You would then have to set the Items/ItemsSource of the ComboBox in the DataTemplate definition. So it would need to be some property or resource accessible to it.

If you want to use the code-behind exclusively (for creating the DataTemplate anyway), then you need to build up the DataTemplate using the FrameworkElementFactory. This factory defines the visual elements that will be created for the associated DataTemplate. So to duplicate the DataTemplate you defined as a XAML resource, you would need to create FrameworkElementFactory for the Grid, then add another FrameworkElementFactory for the ComboBox to it. You could then set properties (explicitly or using bindings) for the ComboBox using the FrameworkElementFactory methods. Then you would need to remove the line where you add to the "dataTemplate" variable's resources.

Having said all that, it seems like you are putting alot of effort in for something that is handled by default. If you simply have a property with a fixed set of values, then there is a much easier method of accomplishing this:
PropertyGridPropertyItem item = new PropertyGridPropertyItem();
item.DisplayName = "MyComboBox";
item.Value = 1;
item.StandardValues = new int[] { 1, 2, 3 };
item.IsLimitedToStandardValues = true; // Only allow values from StandardValues
item.Description = "This is sample combobox property";
item.Category = "My Properties:";
item.ValueTemplate = (DataTemplate)this.TryFindResource(BuiltinEditors.ComboBoxValueTemplateKey);
Hope this helps.


Actipro Software Support

Posted 15 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Actually, the last line of my code sample is not needed as it will be hosted in a ComboBox by default if there are standard values defined.


Actipro Software Support

The latest build of this product (v24.1.1) was released 2 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.