I am a c++ developer and recently shifted to this amazing WPF world. I am developing a WPF application. I have a combobox (BusRate) in my xaml file.
<ComboBox Height="23" ItemsSource="{Binding BusRateList}" SelectedIndex="2" Name="comboBox2" Width="85" />
My Viewmodel class has the following property:
public ObservableCollection<int> _busRate = new ObservableCollection<int>();
public ObservableCollection<int> BusRateList
{
get { return _busRate; }
set
{
_busRate = value;
NotifyPropertyChanged("BusRateList");
}
}
And I add the items as follows:
_busRate.Add(10);
_busRate.Add(50);
_busRate.Add(100);
_busRate.Add(200);
_busRate.Add(300);
_busRate.Add(400);
_busRate.Add(500);
_busRate.Add(600);
This gives me the items in my combobox. But I want to add these items into my combobox in the form of a array which holds all these values. E.g.:
// C++ Code
static const char *busRate[8] =
{
" 10", " 50", "100", "200", "300", "400", "500", "600"
};
thus allowing me to perform the following operation oncomboboxitemselected method:
- Get the SelectedId from the Combobox and store it in a integer variable.
-
pass this integer varaible. Demonstration as follows:
int id = comboBox->getSelectedId(); // C++ Code unsigned long speed = String(busRate[id-1]).getIntValue(); // C++ Code
How can I achieve this in my application 🙂
You can create a new property in your ViewModel of type int and a converter which converts a string to an int value. Now you bind the new property to the ComboBox.SelectedItem-property:
EDIT: