我对C#Visual Studio中的comboBox有一个非常奇怪的问题。
我有这段代码
private void cmbType_SelectedIndexChanged(object sender, EventArgs e)
{
if(cmbType.Text == "tiger")
{
chk1.IsChecked = true;
}
}
如果我选择老虎没有任何反应,但是当我选择下面的那个时,它是"熊" ..它会检查方框
答案 0 :(得分:1)
试试这个:
private void cmbType_SelectedIndexChanged(object sender, EventArgs e)
{
if((string) cmbType.SelectedItem == "tiger")
{
chk1.IsChecked = true;
}
}
答案 1 :(得分:0)
您应该根据索引检查组合框项目,因为所选索引后的文本更改
private void cmbType_SelectedIndexChanged(object sender, EventArgs e)
{
if(cmbType.SelectedText.ToString() == "tiger")
{
chk1.IsChecked = true;
}
}
答案 2 :(得分:0)
private void cmbType_SelectedIndexChanged(object sender, EventArgs e)
{
if(cmbType.SelectedValue.ToString() == "tiger")
{
chk1.Checked = true;
}
}
答案 3 :(得分:0)
你可以这样做
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var comboBoxItem = e.AddedItems[0] as ComboBoxItem;
if (comboBoxItem == null) return;
var content = comboBoxItem.Content as string;
if (content != null && content.Equals("tiger"))
{
cbAnimal.IsChecked = true;
}
}
此致