【问题标题】:Switch case ComboBox did not work properlySwitch case ComboBox 无法正常工作
【发布时间】:2015-12-04 03:55:12
【问题描述】:

我想在每次更改ComboBox 时更改我的TextBox 中的值,这是我的代码,仅适用于ComboBox 的默认SelectedValue,当我更改ComboBox @987654326 时@ 没有改变。

SelectComp_ComboBox.ItemsSource = componentNames;
SelectComp_ComboBox.SelectedValue = componentList[1].componentName;
switch (SelectComp_ComboBox.SelectedValue.ToString())
{
    case "02":
        Status_textBlock.Text = componentList[1].componentStatus;
        Width_textBlock.Text = componentList[1].componentWidth;
        Height_textBlock.Text = componentList[1].componentHeight;
        break;
    case "03":
        Status_textBlock.Text = componentList[2].componentStatus;
        Width_textBlock.Text = componentList[2].componentWidth;
        Height_textBlock.Text = componentList[2].componentHeight;
        break;
    case "04":
        Status_textBlock.Text = componentList[3].componentStatus;
        Width_textBlock.Text = componentList[3].componentWidth;
        Height_textBlock.Text = componentList[3].componentHeight;
        break;
}

每当我更改 ComboBox 时,有什么方法可以让我的状态、宽度和高度 Texboxes 发生变化?

【问题讨论】:

  • 在你的代码执行switch case之前,请输入“MessageBox.Show(SelectComp_ComboBox.SelectedValue.ToString());”因此,我们可以确定将哪些数据传递到 switch case。我最初的想法是你应该使用 SelectedIndex 而不是 SelectedValue 因为你的案例陈述包含数字。
  • @Brad 组合框项目源是由 02、03、04 等组成的字符串列表。我将所选值设置为 02 开始
  • 将您的 switch-case 转换为您的 ComboBox 的 SelectionChanged 事件处理程序。还要在那里检查 SelectedValue 的空值。
  • @AnjumSKhan hm 我把代码放在这个上面:public editA (List<ClassA> componentList) : this() { 所以我不能使用列表到 SelectionChanged,我试图将 componentList 的值添加到新列表但仍然不能。有什么线索吗?
  • @Mirza 无论如何,您都必须处理 ComboBox 的 SelectionChanged 事件。您也可以尝试使用行为。

标签: c# wpf combobox switch-statement


【解决方案1】:

首先,您在这里没有使用任何 wpf 优势。多学习。对于快速而肮脏的答案:

  1. 使用您的组件列表设置combobox.itemssource。
  2. 在组合框 xaml 上使用 DataTemplate 来显示组件名称。
  3. 订阅组合框的选定索引更改事件
  4. 将组合框的选定项转换为您的 componentList 类型。
  5. 使用上述对象设置文本框的值。

但这就像用显微镜做锤子一样。可能,但太错误了。学习

  1. MVVM
  2. 数据模板、绑定等 WPF 原则

我没有侮辱。但说真的,现在你的代码看起来像是上世纪 90 年代的。

【讨论】:

  • 我现在真的不需要 MVVM,需要在不绑定的情况下解决它
  • 你错了。如果你想构建一些有用的东西,你需要学习 MVVM。
猜你喜欢
  • 2012-06-06
  • 2018-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-06
  • 1970-01-01
  • 1970-01-01
  • 2021-08-02
相关资源
最近更新 更多