I am a C++ developer and just started working on WPF. Following MVVM, I am working on comboboxes where I have to add items in it. Well adding items in it seems to be quite easy but I have come across a simple issue where I am not able to figure out what to do. Here is the code:
XAML:
<ComboBox Grid.Row="0" ItemsSource="{Binding DaughterBoardBoxList}" SelectedItem="{Binding SelectedDaughterBoardBoxList, Mode=TwoWay}" SelectedIndex="0" />
<ComboBox Grid.Row="1" ItemsSource="{Binding DaughterVersionBoxList}" SelectedItem="{Binding SelectedDaughterVersionBoxList, Mode=TwoWay}" SelectedIndex="0" />
<ComboBox Grid.Row="2" ItemsSource="{Binding DaughterSerialBoxList}" SelectedItem="{Binding SelectedDaughterSerialBoxList, Mode=TwoWay}" SelectedIndex="0" />
ViewModel Class:
public ObservableCollection<string> DaughterBoardBoxList
{
get { return _DBoardBoxList; }
set
{
_DBoardBoxList = value;
OnPropertyChanged("DaughterBoardBoxList");
}
}
public string _SelectedDBoardBoxList;
public string SelectedDaughterBoardBoxList
{
get { return _SelectedDBoardBoxList; }
set
{
_SelectedDBoardBoxList = value;
OnPropertyChanged("SelectedDaughterBoardBoxList");
}
}
// Similarly for other 2 comboboxes
I have added items in each combobox as follows:
- DaughterBoardBoxItems = “S1010013”, “S1010014”, “S1010015” etc
- DaughterVersionBoxItems = “001A”, “001B”, “001C” etc
- DaughterSerialBoxItems = 1 to 499
I Have added from 1 – 499 as follows:
for (int j = 1; j < 500; j++)
{
_DSerialBoxList.Add(j.ToString());
}
Now on performing some operation I need to execute few statements which are as follows:
String daughterBoard = "S1010015001A0477"; // Hardcoded value for check
String boardName = daughterBoard.Substring(0, 8);
DaughterBoardBoxList = boardName;
String version = daughterBoard.Substring(8, 12);
DaughterVersionBoxList = version;
int serialvalue = Convert.ToInt32(daughterBoard.Substring(12, 16));
String serialNo = Convert.ToString(serialvalue);
DaughterSerialBoxList = serialNo;
longlabel += daughterBoard;
When I execute the above code, it throws me exception at String version = daughterBoard.Substring(8, 12); as Argumentoutofrange. Index and length must refer to a location within the string.
Requirement:
-
Now this where I am confused how do I set the values present in
boardName,versionandserialNoin the combobox without having to face any exception. I don think combobox has settext property. Is their an other alternative solution to achieve this? -
Even though I enter values from 0 to 499, it should appear as 4 digit value i.e. if 77 is added, it should appear as 0077. As I have mentioned in the above code.
Please help 🙂
Firstly the exception is coming because you are stepping out of the string bounds.
String version = daughterBoard.Substring(8, 4)..is what you are after, there isn’t 12 characters available after the 8th. The second parameter is the length you require from the first parameter, not the start.
Then do a check to see if the string is in the list.
Setting the SelectedDaughterVersionBoxList will apply it to the combobox.
A two way binding, as you have done with the selected item, is the best way to set the selected item of a listbox.
There a a couple of ways to format the text you wish to display. Sometimes you can use the StringFormat attribute in xaml. The other is to use a converter.
The shortcut in your case would be to format your string as you populate the list.
That will make sure you always get 4 digits in your Combobox. Look here for more info on it. If the situation required you to actually process the list items as numeric values then you would be better off converting your ObservableCollection to ObservableCollection and using a converter as I mentioned before.