【发布时间】:2014-05-27 14:46:34
【问题描述】:
我是 Silverlight 开发人员,使用 C# 编写代码以从列表中选择一个项目并在附近的 textBlock 中显示所选项目。
我这样做的代码是:
ListBox lines = new ListBox();
TextBlock txtblkShowSelectedValue = new TextBlock();
ScrollViewer scrollViewer = new ScrollViewer();
scrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Visible;
lines.ItemsSource = param.Component.Attributes.Items;
Grid.SetColumn(lines, 1);
Grid.SetRow(lines, LoopCount);
childGrid.Children.Add(lines);
lines.SelectedIndex = 0;
lines.SelectedItem = param.Component.Attributes.Items;
问题是如何选择一个值以及如何在文本块“txtblkShowSelectedValue”中显示它?因为如果我使用 selectionChange 事件,由于当前条件,我无法全局声明 textblock 和 List 变量
编辑:当前情况是:(行(列表)在不同的函数中,所以它不在List_SelectionChanged()函数的范围内)
private static Grid GenerateList(Parameter param, int LoopCount, Grid g)
{
Grid childGrid = new Grid();
ColumnDefinition colDef1 = new ColumnDefinition();
ColumnDefinition colDef2 = new ColumnDefinition();
ColumnDefinition colDef3 = new ColumnDefinition();
childGrid.ColumnDefinitions.Add(colDef1);
childGrid.ColumnDefinitions.Add(colDef2);
childGrid.ColumnDefinitions.Add(colDef3);
TextBlock txtblk1ShowStatus = new TextBlock();
TextBlock txtblkLabel = new TextBlock();
ListBox lines = new ListBox();
ScrollViewer scrollViewer = new ScrollViewer();
scrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Visible;
lines.ItemsSource = param.Component.Attributes.Items;
Grid.SetColumn(lines, 1);
Grid.SetRow(lines, LoopCount);
childGrid.Children.Add(lines);
lines.SelectedIndex = 0;
lines.SelectedItem = param.Component.Attributes.Items;
lines.SelectionChanged += new SelectionChangedEventHandler(List_SelectionChanged);
lines.SelectedIndex = lines.Items.Count - 1;
g.Children.Add(childGrid);
return (g);
}
static void List_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
MessageBox.Show("clist _SelectionChanged1");
TextBlock txtblk1ShowStatus = new TextBlock();
txtblk1ShowStatus.Text = lines[(sender as ListBox).SelectedIndex];
}
【问题讨论】:
-
为 Listbox 的 SelectionChanged 事件添加一个函数。并在此函数中写入:
this.txtblkShowSelectedValue.Text=this.lines[(sender as Listbox).SelectedIndex]其中 sender 是函数的一对二参数 -
@angel 你知道只使用 c# 代码的任何方法吗? Otherthen selectiONcHANGED EVENT ?
标签: c# silverlight listbox silverlight-5.0 selecteditem