0

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

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Sss
  • 1,519
  • 8
  • 37
  • 67

1 Answers1

1

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;
Fireboyd78
  • 103
  • 7
  • THANKS BUT : Error 1 Cannot apply indexing with [] to an expression of type 'System.Windows.Controls.ListBox' corresponding lines[(o as ListBox).SelectedIndex]; – Sss May 28 '14 at 08:17
  • You'll definitely need to edit that line. Is the listbox just "lines"? If so, you could probably use this instead: `txtblk1ShowStatus.Text = lines[lines.SelectedIndex];` – Fireboyd78 May 28 '14 at 08:19
  • Actually, upon re-evaluating the code, I'm not quite sure what's going on. Hopefully you can resolve this issue - best of luck to you! – Fireboyd78 May 28 '14 at 08:21
  • Ok i done it :txtblk1ShowStatus.Text = lines.SelectedItem.ToString(); – Sss May 28 '14 at 08:22
  • please change your code so that i willmark it as answer. – Sss May 28 '14 at 08:22
  • Why do you insist on using the 'lines' ListBox variable in the SelectionChanged handler? The ListBox is passed as the first argument to the event handler. Strange. – Number8 May 28 '14 at 11:14