gis
gis

Reputation: 153

Combobox values generated dynamically

Is it possible to generate dynamically (eg. getting from database) combobox items that is embedded in WPF Toolkit PropertyGrid? I found the following code, but it generates fixed values.

public class Person
{
    [ItemsSource(typeof(FontSizeItemsSource))]
    public double WritingFontSize { get; set; }
}

public class FontSizeItemsSource : IItemsSource
{
    public ItemCollection GetValues()
    {
        ItemCollection sizes = new ItemCollection();
        sizes.Add(5.0, "Five");
        sizes.Add(5.5);
        return sizes;
    }
}

Upvotes: 2

Views: 648

Answers (1)

Anton Danylov
Anton Danylov

Reputation: 1491

You may set your own editing template and provide items to ComboBox in it with binding to ItemsSource:

public class Person
{
    public double WritingFontSize { get; set; }

    public ObservableCollection<double> FontSizeItemsSource
    {
        get
        {
            ObservableCollection<double> sizes = new ObservableCollection<double>();

            // Items generation could be made here
            sizes.Add(5.0);
            sizes.Add(5.5);
            return sizes;
        }

    }
}


    <xctkpg:PropertyGrid SelectedObject="{Binding MyPersonObject}" AutoGenerateProperties="False">
        <xctkpg:PropertyGrid.EditorDefinitions>
            <xctkpg:EditorTemplateDefinition TargetProperties="WritingFontSize">
                <xctkpg:EditorTemplateDefinition.EditingTemplate>
                    <DataTemplate>
                        <ComboBox ItemsSource="{Binding Instance.FontSizeItemsSource}" SelectedValue="{Binding Instance.WritingFontSize}" />
                    </DataTemplate>
                </xctkpg:EditorTemplateDefinition.EditingTemplate>
            </xctkpg:EditorTemplateDefinition>
        </xctkpg:PropertyGrid.EditorDefinitions>

        <xctkpg:PropertyGrid.PropertyDefinitions>
            <xctkpg:PropertyDefinition TargetProperties="WritingFontSize" />
        </xctkpg:PropertyGrid.PropertyDefinitions>
    </xctkpg:PropertyGrid>

Upvotes: 2

Related Questions