【发布时间】: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