假如wpf combobox中控件中有三项, “第一”, “第二”,“第三”, 怎样通过代码设置选中的是"第二"这个item?

解决方案 »

  1.   

                ComboBox a = new ComboBox();
                a.SelectedIndex = 1;注意下标是0开始
    -1为无选中
      

  2.   

    selectvalue 具体怎么做啊, 我将里面的内容输出来, 结果是这个样子的:
    System.Windows.Controls.ComboBoxItem: 第一
    我刚学这个东西, 很菜, 希望高手指点, 哈哈
      

  3.   

    SelectedValue.ToString()  try a try
      

  4.   

    toString()不行哦, 一样的
      

  5.   

    知道这三项的内容么?  直接SelectedValue=value;
      

  6.   

    SelectedValue 属性 
    设置通过使用 SelectedValuePath 而获取的 SelectedItem 的值。
      

  7.   

    首先设置ComboBox的SelectedValuePath属性为ComboBoxItem中某个属性(比如Content或Tag属性),然后再将ComboBox.SelectedValue设置为某个待选中ComboBoxItem的Content属性的值。
    比如:
    <Combobox x:Name="cmbTest" SelectedValuePath="Tag">
       <ComboBoxItem Content="测试一" Tag="test1">
       <ComboBoxItem Content="测试二" Tag="test2">
       <ComboBoxItem Content="测试三" Tag="test3">
       <ComboBoxItem Content="测试四" Tag="test4">
    </ComboBox>
    cs代码:
    cmbTest.SelectedValue = test1;  //此时选中第一项【测试一】