我是一名 C++ 开发人员,刚刚开始研究 WPF。在 MVVM 之后,我正在处理组合框,我必须在其中添加项目。好吧,在其中添加项目似乎很容易,但我遇到了一个简单的问题,我无法弄清楚该怎么做。这是代码:
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" />
视图模型类:
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
我在每个组合框中添加了如下项目:
- DaughterBoardBoxItems = “S1010013”、“S1010014”、“S1010015”等
- DaughterVersionBoxItems = “001A”、“001B”、“001C”等
- DaughterSerialBoxItems = 1 到 499
我从 1 - 499 添加如下:
for (int j = 1; j < 500; j++)
{
_DSerialBoxList.Add(j.ToString());
}
现在在执行一些操作时,我需要执行一些语句,如下所示:
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;
当我执行上面的代码时,它会抛出String version = daughterBoard.Substring(8, 12);
异常Argumentoutofrange. Index and length must refer to a location within the string.
要求:
现在,我很困惑如何设置 和组合框中的值,
boardName
而不必面对任何异常。我不认为组合框有 settext 属性。他们是实现这一目标的另一种替代解决方案吗?version
serialNo
即使我输入从 0 到 499 的值,它也应该显示为 4 位值,即如果添加 77,它应该显示为 0077。正如我在上面的代码中提到的。
请帮忙 :)