Binding PropertyGridPropertyItem's Value to multiple enumerators

Grids for WPF Forum

Posted 3 years ago by IneedHelp
Version: 16.1.0632
Avatar

Hello,

How to bind in code behind a PropertyGridPropertyItem's Value to a HorizontalListBox's SelectedItems property where the HorizontalListBox data template has SelectionMode set to Multiple.

Basically I want a property like the one displayed in the "SampleBrowser\ProductSamples\PropertyGridSamples\QuickStart\CustomPropertyEditor" demo, but with the ability to have multiple selection for the property's value.

Among numerous trial and error attempts, I've tried this too, but it throws an exception because the SelectedItemsProperty has no setter through binding.

_propertyGrid = new PropertyGrid();

SnapToPanels = new PropertyGridPropertyItem
            {
                //ValueType = HorizontalListBox.SelectedItemsProperty.GetType(),
                ValueType = typeof(List<OnOffAuto>),
                //Value = OnOffAuto.Off,
                //DefaultValue = OnOffAuto.On,
            };

var dataTemplate = new DataTemplate();
var elementFactory = new FrameworkElementFactory(typeof(MyHorizontalListBox));

var binding2 = new Binding
            {
                Source = elementFactory,
                Path = new PropertyPath(path: MyHorizontalListBox.SelectedItemsProperty.ToString()),
                Mode = BindingMode.TwoWay,
            };
            SnapToPanels.SetBinding(PropertyGridPropertyItem.ValueProperty, binding: binding2);

            dataTemplate.VisualTree = elementFactory;

            SnapToPanels.ValueTemplate = dataTemplate;
internal class MyHorizontalListBox : HorizontalListBox
{
public MyHorizontalListBox()
{
this.SelectionMode = System.Windows.Controls.SelectionMode.Multiple;

Items.Add(OnOffAuto.Auto);
Items.Add(OnOffAuto.Off);
Items.Add(OnOffAuto.On);

}
}

   public enum OnOffAuto
        {
            /// <summary>
            /// Automatic.
            /// </summary>
            Auto,

            /// <summary>
            /// On.
            /// </summary>
            On,

            /// <summary>
            /// Off.
            /// </summary>
            Off
        }

 

 Please help, thank you.

[Modified 3 years ago]

Comments (2)

Answer - Posted 3 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hello,

The HorizontalListBox is just a different style for a normal ListBox but it operates the same.  Here it seems like you want to change the enum to be a [Flags] enum and then look at posts like this that talk about binding a flags enum to a ListBox:

http://stackoverflow.com/questions/3474924/how-to-bind-flags-enums-to-listbox-in-mvvm


Actipro Software Support

Posted 3 years ago by IneedHelp
Avatar

Thank you for the information.

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

Add Comment

Please log in to a validated account to post comments.