Select to view content in your preferred language

ComboBox with predefine string values in dockpane

283
4
a week ago
ModyBuchbinder
Esri Regular Contributor

I am trying to put a ComboBox in my dockpane with pre defined string values (Something like Polygon/Polyline/Point). They never change.

All the examples I found have ObservableCollection with some classes or other complex behavior that I do not need.

My ComboBox is coming out empty.

Anybody have very simple example on how to do it?

Thanks

 

0 Kudos
4 Replies
GKmieliauskas
Esri Regular Contributor

Hi,

You can do it from xaml:

<ComboBox Height="23" Name="comboBox1" Width="120">
    <ComboBoxItem Content="Polygon"/>
    <ComboBoxItem Content="Polyline"/>
    <ComboBoxItem Content="Point"/>
</ComboBox>
0 Kudos
ModyBuchbinder
Esri Regular Contributor

Already done this.

It does not looks like MVVM - is that the only way?

0 Kudos
GKmieliauskas
Esri Regular Contributor

XAML:

            <ComboBox ItemsSource="{Binding GeometryTypes}" SelectedItem="{Binding SelectedGeometryType, Mode=TwoWay}" Text="Select Option" />

ViewModel:

        public List<string> GeometryTypes => new List<string>() { "Polygon", "Polyline", "Point" };

        public string SelectedGeometryType { get; set; } = "Polygon";
0 Kudos
ModyBuchbinder
Esri Regular Contributor

After playing a lot (and consult AI) this is my solution

XML

<ComboBox x:Name="types"  DisplayMemberPath="Name"  ItemsSource="{Binding TypesItems}" SelectedItem="{Binding SelectedType, Mode=TwoWay}"/>

Helper class:

public class MyItemClass
{
    public string Name { get; set; }
}

Fill the list

 private ObservableCollection<MyItemClass> _TypesItems;

// fill the list in ctor
           TypesItems = new ObservableCollection<MyItemClass>
       {
           new MyItemClass {  Name = "Item 1" },
           new MyItemClass {  Name = "Item 2" },
           new MyItemClass {  Name = "Item 3" },
       };
 public ObservableCollection<MyItemClass> TypesItems
 {
     get => _TypesItems;
     set => SetProperty(ref _TypesItems, value);
 }

and finally get the selected

       private MyItemClass _selectedType;
       public MyItemClass SelectedType
       {
           get => _selectedType;
           set => SetProperty(ref _selectedType, value);
       }