在Silverlight中使用到了“Comobox控件”,其“四种”用法如下:一、不绑定数据源。前台:
<ComboBox x:Name="cb_chartType" Canvas.Left="87" Canvas.Top="39" Width="96" SelectionChanged="cb_chartType_SelectionChanged">
  <ComboBoxItem Content="柱形图" IsSelected="True" />
  <ComboBoxItem Content="堆积图"/>
  <ComboBoxItem Content="线形图"/>
</ComboBox>后台:
 private void cb_chartType_SelectionChanged(object sender, SelectionChangedEventArgs e)
  {
  //string type = cb_chartType.SelectedValue.ToString();  //ComboBox box = sender as ComboBox;
  TextBlock tbl = cb_chartType.SelectedItem as TextBlock;问题一:在“SL”页面加载时提示错误,“未将对象引用设置到对象的实例。”,请问这是为什么?
不绑定数据源不能使用吗?这种情况下,如何获取其“ComboBoxItem”的“Content”的值???
  string type = tbl.Text;  DataSeries dataSeries = SLChart.Series[0];
  switch (type)
  {
  case "柱形图":
  dataSeries.RenderAs = RenderAs.Column;
  break;
  case "线性图":
  dataSeries.RenderAs = RenderAs.Line;
  break;
  //case "Pie":
  // dataSeries.RenderAs = RenderAs.Pie;
  // break;
  //case "Bar":
  // dataSeries.RenderAs = RenderAs.Bar;
  // break;
  case "堆积图":
  dataSeries.RenderAs = RenderAs.Area;
  break;
  }
  }
二、不绑定数据源,修改“ComboBoxItem”。
前台改为:   <ComboBox x:Name="cb_chartType" Canvas.Left="87" Canvas.Top="39" Width="96" SelectionChanged="cb_chartType_SelectionChanged">
                <ComboBoxItem  IsSelected="True"  >
                    <ComboBoxItem.Content>
                        <TextBlock>柱形图</TextBlock>
                    </ComboBoxItem.Content>
                </ComboBoxItem>
                <ComboBoxItem >
                    <ComboBoxItem.Content>
                        <TextBlock>堆积图</TextBlock>
                    </ComboBoxItem.Content>
                </ComboBoxItem>
                <ComboBoxItem >
                    <ComboBoxItem.Content>
                        <TextBlock>线形图</TextBlock>
                    </ComboBoxItem.Content>
                </ComboBoxItem>
            </ComboBox>问题二:和第一中方式一样。在“SL”页面加载时提示错误,“未将对象引用设置到对象的实例。”,请问这是为什么?
不绑定数据源不能使用吗?这种情况下,如何获取其“ComboBoxItem”的“Content”的值???

三、绑定数据源,数据集合<List>。问题三:下面的方法使用了数据源,但是“Combobox(Silverlight控件)”根本就没有绑定上啊,内容为空的。请问这是怎么回事???
public partial class ElecMonitoring : UserControl
  {
List<SLChartType> slcharttypes = new List<SLChartType>();
  public ElecMonitoring()
  {
  InitializeComponent();
  BindCombox();
  }  private void BindCombox()
  {
  SLChartType slcharttype1 = new SLChartType (){ text="柱形图", value="Column" };
  SLChartType slcharttype2 = new SLChartType (){ text="堆积图", value="Area" };
  SLChartType slcharttype3 = new SLChartType() { text = "线形图", value = "Line" };
  slcharttypes.Add(slcharttype1);
  slcharttypes.Add(slcharttype2);
  slcharttypes.Add(slcharttype3);
  cb_chartType.ItemsSource = slcharttypes;
  cb_chartType.SelectedValuePath = "Value";
  cb_chartType.DisplayMemberPath = "Test";
  cb_chartType.SelectedIndex = 0;
  }
  }public class SLChartType
  {
  public string text { get; set; }
  public string value { get; set; }
  }四、使用“TextBlock”控件。问题四:而使用下面这种方式,使用“TextBlock”却可以,请问这是为什么呢?private void BindCombox()
  {
  //SLChartType slcharttype1 = new SLChartType (){ text="柱形图", value="Column" };
  //SLChartType slcharttype2 = new SLChartType (){ text="堆积图", value="Area" };
  //SLChartType slcharttype3 = new SLChartType() { text = "线形图", value = "Line" };
  //slcharttypes.Add(slcharttype1);
  //slcharttypes.Add(slcharttype2);
  //slcharttypes.Add(slcharttype3);
  //cb_chartType.ItemsSource = slcharttypes;
  //cb_chartType.SelectedValuePath = "Value";
  //cb_chartType.DisplayMemberPath = "Test";
  //cb_chartType.SelectedIndex = 0;    
  TextBlock tbl1 = new TextBlock();
  tbl1.Text = "柱形图";
  cb_chartType.Items.Add(tbl1);
  TextBlock tbl2 = new TextBlock();
  tbl2.Text = "堆积图";
  cb_chartType.Items.Add(tbl2);
  TextBlock tbl3 = new TextBlock();
  tbl3.Text = "线性图";
  cb_chartType.Items.Add(tbl3);
  cb_chartType.SelectedIndex = 0;
  }

解决方案 »

  1.   

    问题一,,  TextBlock tbl = cb_chartType.SelectedItem as TextBlock;这句代码错了嘛
      

  2.   

     private void cb_chartType_SelectionChanged(object sender, SelectionChangedEventArgs e)
       {
        if(cb_chartType!=null)
         {
           string type = cb_chartType.SelectedValue.ToString();     }
       }这个事件发生时,对象没有加载完成,导致cb_chartType为null
      
      

  3.   

    问题三
    text="柱形图", value="Column"cb_chartType.SelectedValuePath = "Value";
    cb_chartType.DisplayMemberPath = "Test";你new的时候是小写
    在下面又是大写
    人家反射不到的,你不要难为人家啊
      

  4.   

    问题一:
    第一次触发onchanged的时候count=0,所以判断一下cbox的中Items>0方可操作
    问题二同问题一在明确一二三后第四个问题就不用回答了吧
      

  5.   


    第一次触发的时候,“count=0”,不是已经在“XAML”文件中设定了吗?
      

  6.   


    原因找到了,原来是“text”写成“Test”了,而且大小写不一致。但是,还是有点问题诶。“ItemsSource指定数据源,SelectedValuePath指定选定值字段,DisplayMemberPath指定显示字段;SelectedItem获取或设置选定的数据源的项,SelectedValue获取或设置选定的值(由SelectedValuePath决定)”“DisplayMemberPath指定显示字段”,那么“显示字段text”的值怎样获取呢???
      

  7.   

    (SelectedItem as SLChartType).Text
      

  8.   


    晕!明明是 as ComboBoxItem啊。例如var x= Sender as BoxboBoxItem;
    if(x !=null && x.Content is string)
    {
        var str= (string)x.Content;
        .....
    }
      

  9.   


    谢谢您,就是这样的。string str = (cb_chartType.SelectedItem as SLChartType).text.ToString();在这里,想多问一点。初始化“List<SLChartType> slcharttypes = new List<SLChartType>();”,在这里分为了两步。
    1、List<SLChartType> slcharttypes = new List<SLChartType>();
    2、
    SLChartType slcharttype1 = new SLChartType (){ text="柱形图", value="Column" };
      SLChartType slcharttype2 = new SLChartType (){ text="堆积图", value="Area" };
      SLChartType slcharttype3 = new SLChartType() { text = "线形图", value = "Line" };
      slcharttypes.Add(slcharttype1);
      slcharttypes.Add(slcharttype2);
      slcharttypes.Add(slcharttype3);问题:有没有“一步”初始化“slcharttypes”集合的方法?
      

  10.   

    实际上当你明明知道它是ComboxBoxItem,并且其Content明明是string时,你也无需去检测,直接强制类型转换就行了,反而不会有任何性能损失(因为它确实就是那类对象,用不着什么转换)。
      

  11.   

    在Silverlight中使用到了“Comobox控件”  这个东西似乎不太对啊
      

  12.   

    ComboxBox的Items本来就是一个object集合,可能是任何类型的值。假设你已经弥明是声明了ComboxBoxItem,白纸黑字在那里写清楚了,你自己又搞什么转换为TextBlock,你觉得可能吗?
      

  13.   


    对吭,“BoxboBoxItem”吭
    “BoxboBoxItem” 不能转化为“TextBlock”吗?
      

  14.   


    激动了,激动了。谢谢tring str = (cb_chartType.SelectedItem as SLChartType).text.ToString();在这里,想多问一点。初始化“List<SLChartType> slcharttypes = new List<SLChartType>();”,在这里分为了两步。
    1、List<SLChartType> slcharttypes = new List<SLChartType>();
    2、
    SLChartType slcharttype1 = new SLChartType (){ text="柱形图", value="Column" };
    SLChartType slcharttype2 = new SLChartType (){ text="堆积图", value="Area" };
    SLChartType slcharttype3 = new SLChartType() { text = "线形图", value = "Line" };
    slcharttypes.Add(slcharttype1);
    slcharttypes.Add(slcharttype2);
    slcharttypes.Add(slcharttype3);问题:有没有“一步”初始化“slcharttypes”集合的方法?
      

  15.   

    这样就好了,谢谢各位。
    List<SLChartType> slcharttypes = new List<SLChartType> {
    new SLChartType() { text = "柱形图", value = "Column" }, 
    new SLChartType() { text = "堆积图", value = "Area" }, 
    new SLChartType() { text = "线形图", value = "Line" }
    };