首页 新闻 会员 周边

SL下"ComboBox"的“四种使用方式”,谁能给答案???

0
悬赏园豆:40 [已解决问题] 解决于 2012-03-05 22:00

在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;
  }

[CC]的主页 [CC] | 初学一级 | 园豆:61
提问于:2012-02-28 17:18
< >
分享
最佳答案
0

第一、第二不绑定数据源,是不可以其中的数据的,所以报错。

第三 cb_chartType.DisplayMemberPath = "Test";这句话错了,要改为text,因为你的对象中没有这个属性。

收获园豆:40
az235 | 大侠五级 |园豆:8483 | 2012-02-28 18:14

原因找到了,原来是“text”写成“Test”了,而且大小写不一致。但是,还是有点问题诶。

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

1、“DisplayMemberPath指定显示字段”,那么“显示字段text”的值怎样获取呢???

2、静态设置,不绑定数据源不行,取不到数据???

[CC] | 园豆:61 (初学一级) | 2012-02-28 21:22

在这里,想多问一点。初始化“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”集合的方法?

[CC] | 园豆:61 (初学一级) | 2012-02-28 21:48

@星空(StarrySky):  这里不知道你说的是不是这个意思  :

可以使用【集合初始化器】:

List<SLChartType> slcharttypes = new List<SLChartType>(){

 new SLChartType (){ text="柱形图", value="Column" },

 new SLChartType (){ text="堆积图", value="Area" },

new SLChartType() { text = "线形图", value = "Line" }

};

这样就可以实现一步了:起始就是两步合在一起了

DuFeng | 园豆:270 (菜鸟二级) | 2012-02-28 23:11
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册