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
Hi,
You can do it from xaml:
<ComboBox Height="23" Name="comboBox1" Width="120">
<ComboBoxItem Content="Polygon"/>
<ComboBoxItem Content="Polyline"/>
<ComboBoxItem Content="Point"/>
</ComboBox>
Already done this.
It does not looks like MVVM - is that the only way?
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";
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);
}