不行,怎么var selectedUnit = (string)Unit_Selection.SelectedItem; if (selectedUnit == "MaterialStandard_BZUnit")

动态 未结 0 96
小小草
小小草 LV1 2023年9月23日 23:40 发表
如果你想要检查 `ComboBox` 中选定项的字符串内容是否等于特定值,你可以像你在问题中提到的那样使用 `if` 语句。但是,在这种情况下,你需要将 `SelectedItem` 强制转换为 `string` 类型,而不是 `ComboBoxItem` 类型。 以下是一个示例: ```csharp private void Unit_Selection_SelectionChanged(object sender, SelectionChangedEventArgs e) { var selectedUnit = (string)Unit_Selection.SelectedItem; if (selectedUnit == "MaterialStandard_BZUnit") { // 处理选定项等于 "MaterialStandard_BZUnit" 的情况 } } ``` 在这个示例中,我们将 `SelectedItem` 强制转换为 `string` 类型,并将其赋值给一个名为 `selectedUnit` 的变量。然后,我们使用 `if` 语句检查 `selectedUnit` 是否等于特定的字符串值。如果是,我们可以在 `if` 语句块中处理相应的逻辑。 请注意,如果你的 `ComboBox` 中的每个选项都具有不同的字符串值,你需要根据选定项的类型来访问相应的属性。例如,如果你的 `ComboBox` 中包含一个名为 `Name` 的属性,你可以这样处理: ```csharp private void Color_Selection_SelectionChanged(object sender, SelectionChangedEventArgs e) { var selectedColor = (MyColorClass)Color_Selection.SelectedItem; if (selectedColor != null && selectedColor.Name == "Red") { // 处理选定项的 Name 属性等于 "Red" 的情况 } } ``` 在这个示例中,我们将 `SelectedItem` 强制转换为 `MyColorClass` 类型,并检查它是否为 `null`。然后,我们使用 `selectedColor.Name` 属性来访问选定项的 `Name` 属性,并检查它是否等于特定的字符串值。如果是,我们可以在 `if` 语句块中处理相应的逻辑。
收藏(0)  分享
相关标签: 智能问答
问题没解决?让AI助手帮你作答 AI助手
0个回复
  • 消灭零回复