Double property Item source for Custom Editor.

Grids for WPF Forum

Posted 8 years ago by Ong Chuan Eng
Avatar
Hi ,
There is double property FontSize in the viewmodel class, which is binded to property grid using selected object.

I have added custom combo editor to the property to display Renage of values.
as below.

private double _fontSize;
[Category("Line Properties")]
[Editor(typeof(CustomDoublePropertyEditor), typeof(PropertyEditor))]
public double FontSize
{
get
{
return _fontSize;
}
set
{
_fontSize = value;
Changed("FontSize");
}
}
public class CustomDoublePropertyEditor : ActiproSoftware.Windows.Controls.Editors.Interop.PropertyGrid.PropertyEditors.DoublePropertyEditor
{
protected override DataTemplate CreateDataTemplate()
{
DataTemplate cardLayout = new DataTemplate();
//set up the stack panel
FrameworkElementFactory spFactory = new FrameworkElementFactory(typeof(DockPanel));
spFactory.Name = "myCustomFactory";

FrameworkElementFactory combo = new FrameworkElementFactory(typeof(ComboBox));
Binding selectedItem = new Binding("Value");
selectedItem.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(ActiproSoftware.Windows.Controls.PropertyGrid.Primitives.PropertyGridDataAccessorItem), 1);
selectedItem.UpdateSourceTrigger = UpdateSourceTrigger.LostFocus;
combo.SetBinding(ComboBox.SelectedItemProperty, selectedItem);

ObservableCollection<double> itemSource = new ObservableCollection<double>();
for (double i = LineViewModel.MinmunFontSize; i <= LineViewModel.MaximumFontSize; i++)
itemSource.Add(i);

combo.SetValue(ComboBox.ItemsSourceProperty, itemSource);

spFactory.AppendChild(combo);
cardLayout.VisualTree = spFactory;

return cardLayout;
}

public CustomDoublePropertyEditor()
{
this.DropDownButtonVisibility = Visibility.Visible;
}
}

Now the issue is the ItemSource of the comboBox should change dymanically.
ItemSource is a Range from MinValue to MaxValue. There are properties as MinmumValue and MaxValue in the same ViewModel Class.

How to Bind the ItemSource of the combo to a property in the same viewmodel class?

Thanks in advance.

[Modified at 08/03/2011 06:50 AM]

Comments (5)

Posted 8 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hello,

Since the DataTemplate can be used for any number of properties, it has to be "generic". As such, you would have to bind the ItemsSource property to your view-model properties. You can't do it inline like you have above.

If you have a single property of type IEnumerable<int> on your view-model, you could do something like:
var binding = new Binding("Target.FontSizeValues") {
   RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(PropertyGridDataAccessorItem), 1)
};
combo.SetBinding(ComboBox.ItemsSourceProperty, binding);
If you want to bind ItemsSource to your two properties MinValue and MaxValue, then you'd have to use a MultiBinding and a custom IValueConverter to generate the list of potential values.


Actipro Software Support

Posted 8 years ago by Ong Chuan Eng
Avatar
Thanks for the reply.

In that case as mention above, if we need to bind the IEnumerable<int> in the viewmodel to the itemsource of Combo and another property int in the same Viewmodel as SelectedItem to the combo, How to merge two properties for single PropertyGridItem.

Thanks in Advance
Posted 8 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hello,

Sorry, I don't understand your question. What two properties are you trying to merge?


Actipro Software Support

Posted 8 years ago by Ong Chuan Eng
Avatar
Hi,

I have a ViewModel Class as collection binded to property grid using selected object.
In the class there is a property IEnumerable<int> (Values) and int ( value)

I need to show a combo as Property in the propertyGrid.
Combo-> ItemSource should be IEnumerable<int> (Values).
Combo-> SelectedItem should be int (Value).

Please let me know is it possible?

Thanks in advance
Posted 8 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hello,

I'm assuming the Value property is being displayed by the PropertyGrid, while the Values property should not be shown. If that's the case, then you'd need to define a custom property editor that uses a ComboBox, much like the one you listed earlier. You can then bind the ComboBox.ItemsSource to the Values property like I showed earlier for FontSizeValues:
var binding = new Binding("Target.Values") {
   RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(PropertyGridDataAccessorItem), 1)
};
combo.SetBinding(ComboBox.ItemsSourceProperty, binding);
You can then bind the value separately like:
var binding = new Binding("Value") {
   RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(PropertyGridDataAccessorItem), 1)
};
combo.SetBinding(ComboBox.SelectedItemProperty, binding);
The same concept applies if you are defining your DataTemplate via XAML.


Actipro Software Support

The latest build of this product (v2019.1 build 0683) was released 1 month ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.