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

starrysilverlight 2012-02-28 02:43:27
在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;
}
}
...全文
306 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
starryplayer 2012-02-29
  • 打赏
  • 举报
回复
这样就好了,谢谢各位。
List<SLChartType> slcharttypes = new List<SLChartType> {
new SLChartType() { text = "柱形图", value = "Column" },
new SLChartType() { text = "堆积图", value = "Area" },
new SLChartType() { text = "线形图", value = "Line" }
};
starryplayer 2012-02-28
  • 打赏
  • 举报
回复
谢谢“nonocast”等网友,就是这样的。

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”集合的方法?
starryplayer 2012-02-28
  • 打赏
  • 举报
回复
原因找到了,原来是“text”写成“Test”了,而且大小写不一致。但是,还是有点问题诶。

“ItemsSource指定数据源,SelectedValuePath指定选定值字段,DisplayMemberPath指定显示字段;SelectedItem获取或设置选定的数据源的项,SelectedValue获取或设置选定的值(由SelectedValuePath决定)”

“DisplayMemberPath指定显示字段”,那么“显示字段text”的值怎样获取呢???
BenBenCode 2012-02-28
  • 打赏
  • 举报
回复
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里啊
starrychat 2012-02-28
  • 打赏
  • 举报
回复
而使用下面这种方式,使用“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;
}
starryplayer 2012-02-28
  • 打赏
  • 举报
回复
这种方法使用了数据源,但是根本就没有绑定上啊。

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; }
}
starryplayer 2012-02-28
  • 打赏
  • 举报
回复
这样也还是不行。
<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>
<CheckBox x:Name="ckb_3D" Content="3D" Canvas.Left="251" Canvas.Top="39" Checked="ckb_3D_Checked" Unchecked="ckb_3D_Unchecked" />
<CheckBox x:Name="ckb_Guides" Content="参考线" Canvas.Left="357" Canvas.Top="39" Width="55" Checked="ckb_Guides_Checked" Unchecked="ckb_Guides_Unchecked" />
<CheckBox x:Name="ckb_Zoom" Content="Zoom" Canvas.Left="509" Canvas.Top="39" Unchecked="ckb_Zoom_Unchecked" Checked="ckb_Zoom_Checked" />
<TextBlock Canvas.Left="716" TextWrapping="Wrap" Text="导出选择:" Canvas.Top="41"/>
<ComboBox x:Name="cb_Export" Canvas.Left="775" Canvas.Top="39" Width="120">
<ComboBoxItem Content="图片" IsSelected="True"/>
<ComboBoxItem Content="PDF"/>
<ComboBoxItem Content="Excel"/>
</ComboBox>
starryplayer 2012-02-28
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 benbencode 的回复:]

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


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

你这个不是加载方法啊
[/Quote]

难道不绑定数据源“Combobox”就不能用了
BenBenCode 2012-02-28
  • 打赏
  • 举报
回复
问题:在“SL”页面加载时提示错误,“未将对象引用设置到对象的实例。”,请问这是为什么?


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

你这个不是加载方法啊
starryplayer 2012-02-28
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 bdmh 的回复:]

cb_chartType_SelectionChanged被触发时box.SelectedItem 是否为空,box.SelectedItem 是否可以被转为textblock,这个要做个容错
[/Quote]

if (cb_chartType.SelectedItem != null)
{
}

这样判断时,“cb_chartType.SelectedItem”直接提示“未将对象引用设置到对象的实例。”了啊。

简单的讲,怎样获取“Combobox”中的“Content”值呢,没有绑定数据源?


bdmh 2012-02-28
  • 打赏
  • 举报
回复
cb_chartType_SelectionChanged被触发时box.SelectedItem 是否为空,box.SelectedItem 是否可以被转为textblock,这个要做个容错
starryplayer 2012-02-28
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 q107770540 的回复:]

if ( cb_chartType.SelectedItem !=null)
TextBlock tbl = cb_chartType.SelectedItem as TextBlock;
[/Quote]

不行啊,大侠。

if (cb_chartType.SelectedItem != null)
问题:“cb_chartType.SelectedItem”这一句直接提示“未将对象引用设置到对象的实例。”。
{
}
starryplayer 2012-02-28
  • 打赏
  • 举报
回复
如果:
private void cb_chartType_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//string type = cb_chartType.SelectedValue.ToString();

ComboBox box = sender as ComboBox;
TextBlock tbl = box.SelectedItem as TextBlock;
问题:“未将对象引用设置到对象的实例。”不提示了,但是“tbl”为“null”,这是为什么呢???

}
q107770540 2012-02-28
  • 打赏
  • 举报
回复
if ( cb_chartType.SelectedItem !=null)
TextBlock tbl = cb_chartType.SelectedItem as TextBlock;

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧