Search code examples
c#.netsilverlightsilverlight-5.0selectionchanged

How to implement my own SelectionChangedEventHandler in c#


I am working in silverlight and under a very strange situation that i need to implement my own SelectionChangedEventHandler handler function .The reason to do so is:

I have my situation like this :

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

Here you can see that i have no access of of "lines" and "txtblk1ShowStatus" in the List_SelectionChanged(object sender, SelectionChangedEventArgs e) function. And i cannot declate the button and list globally because this function GenerateList(...) will be reused and it's just coded in c# (no xaml used).Please let me know how to do that and also explain how to do that if you have another way to do but please explain your code with detail


Solution

  • I think a lambda expression will help you solve your issues (you might need to edit it):

    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;