读书人

“未将对象引用设置到对象的实例。”

发布时间: 2012-03-22 17:43:57 作者: rapoo

“未将对象引用设置到对象的实例。”,Comobox
在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;

[color=#FF0000]问题:在“SL”页面加载时提示错误,“未将对象引用设置到对象的实例。”,请问这是为什么?
[/color]
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;
}
}

[解决办法]
if ( cb_chartType.SelectedItem !=null)
TextBlock tbl = cb_chartType.SelectedItem as TextBlock;

[解决办法]
cb_chartType_SelectionChanged被触发时box.SelectedItem 是否为空,box.SelectedItem 是否可以被转为textblock,这个要做个容错
[解决办法]
问题:在“SL”页面加载时提示错误,“未将对象引用设置到对象的实例。”,请问这是为什么?


应该是在加载的时候里面没有数据

你这个不是加载方法啊

[解决办法]
TextBlock tbl = cb_chartType.SelectedItem as TextBlock;

问题:在“SL”页面加载时提示错误,“未将对象引用设置到对象的实例。”,请问这是为什么?

string type = tbl.Text;


跟赋值没有关系、 本来TextBlock tbl = cb_chartType.SelectedItem as TextBlock;
就是错的 、不可能这么赋值吧
你完全可以直接把cb_chartType.SelectedValue.ToString();
switch (type)
{
放到type里啊

读书人网 >C#

热点推荐