C# 如何在c中实现我自己的SelectionChangedEventHandler#

C# 如何在c中实现我自己的SelectionChangedEventHandler#,c#,.net,silverlight,silverlight-5.0,selectionchanged,C#,.net,Silverlight,Silverlight 5.0,Selectionchanged,我在silverlight工作,在一个非常奇怪的情况下,我需要实现自己的SelectionChangedEventHandler处理程序函数。这样做的原因是: 我的情况是这样的: private static Grid GenerateList(Parameter param, int LoopCount, Grid g) { Grid childGrid = new Grid(); ColumnDefinition colDef1 = new C

我在silverlight工作,在一个非常奇怪的情况下,我需要实现自己的SelectionChangedEventHandler处理程序函数。这样做的原因是:

我的情况是这样的:

    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;
        scrollViewer.Content = lines; 
        Grid.SetColumn(scrollViewer, 1);
        Grid.SetRow(scrollViewer, LoopCount);
        childGrid.Children.Add(scrollViewer);

        lines.SelectedIndex = 0;
        lines.SelectedItem = param.Component.Attributes.Items;// This items contains 1000000,3 00000, and so on.
        lines.SelectionChanged += new SelectionChangedEventHandler(List_SelectionChanged);
       // lines.SelectionChanged += new SelectionChangedEventHandler(List_SelectionChanged(lines, txtblk1ShowStatus));
        lines.SelectedIndex = lines.Items.Count - 1;


        Grid.SetColumn(txtblk1ShowStatus, 2);
        Grid.SetRow(txtblk1ShowStatus, LoopCount);
        childGrid.Children.Add(txtblk1ShowStatus);
        g.Children.Add(childGrid);
        return (g);
    }
   static void List_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        MessageBox.Show("clist   _SelectionChanged1");  
      //The problem is line below because i am "lines" and "txtblk1ShowStatus" are not in the 
      //scope of this function and i cannot declare them globally.
        txtblk1ShowStatus.Text = lines[(sender as ListBox).SelectedIndex]; 
    }  

在这里,您可以看到我无法访问
列表中的“行”和“txtblk1ShowStatus”\u SelectionChanged(对象发送者,SelectionChangedEventArgs e)
功能我不能全局取消按钮和列表,因为这个函数
GenerateList(…)
将被重用,它只是用c#编码的(没有使用xaml).如果您有其他方法,请告诉我如何操作,并解释如何操作,但请详细解释您的代码

我认为lambda表达式将帮助您解决问题(您可能需要编辑它):


谢谢,但是:错误1无法将带[]的索引应用于“System.Windows.Controls.ListBox”类型的表达式对应的行[(o作为ListBox.SelectedIndex];您肯定需要编辑该行。列表框只是“行”吗?如果是这样的话,您可能会改用它:
txtblk1ShowStatus.Text=lines[lines.SelectedIndex]lines.SelectedIndex = 0;
lines.SelectedItem = param.Component.Attributes.Items;// This items contains 1000000,3 00000, and so on.

lines.SelectionChanged += (o,e) => {
    MessageBox.Show("clist   _SelectionChanged1");
    txtblk1ShowStatus.Text = lines.SelectedItem.ToString();
};

lines.SelectedIndex = lines.Items.Count - 1;