0

I am Silverlight developer and coding in C# to select an item from a list and display the selected item in the textBlock nearby.

My code to do so is:

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;

The problem is how to select a value and how to display it in textblock "txtblkShowSelectedValue " ? because I cannot declare textblock and List variable globally because of current condition if I use selectionChange event

EDIT: The current scenario is :(lines (List) is in different function so it's not in scope of List_SelectionChanged() function)

 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];
    }
Sss
  • 1,519
  • 8
  • 37
  • 67
  • 1
    add a function for SelectionChanged event of your Listbox. and in this function write : `this.txtblkShowSelectedValue.Text=this.lines[(sender as Listbox).SelectedIndex]` where sender is one on 2 parameters of your function – angel May 27 '14 at 14:52
  • @angel Do you know any way to do it only using c# code ? Otherthen selectiONcHANGED EVENT ? – Sss May 27 '14 at 14:55

3 Answers3

1
lines.SelectionChanged+=new System.EventHandler(this.UpdateTextBlock); // add selectionchanged even for your listbox;

private void UpdateTextBlock(object sender, SelectionChangedEventArgs e)
{
    txtblkShowSelectedValue.Text=this.lines[(sender as Listbox).SelectedIndex].ToString(); // just edit the content of your texblock
}

EDIT : thank you, and sorry to be late :-)

try this :

add parameter for the function, as this :

    lines.SelectionChanged += new SelectionChangedEventHandler(List_SelectionChanged)

change parameter of this function and set your textblock as this :

static void List_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    MessageBox.Show("clist   _SelectionChanged1");
    txtblkShowSelectedValue.Text=this.lines[(sender as Listbox).SelectedIndex].ToString()
}
angel
  • 322
  • 1
  • 7
  • you can add `txtblkShowSelectedValue.isReadOnly=True // texblock will not be edited by user, only by the function` – angel May 27 '14 at 15:06
  • I have done it but the problem is this "txtblkShowSelectedValue" is in the scope of function which contains this call to "UpdateTextBlock()" selectionchanged event . And i cannot do globally TextBlock txtblkShowSelectedValue = new TextBlock();(because of current situations) so it has problem – Sss May 27 '14 at 15:07
  • Error 1 The name 'lines' does not exist in the current context corresponding to this.txtblkShowSelectedValue.Text=this.lines[(sender as Listbox).SelectedIndex].ToString(); and i cannot decalre it it globally because of current situation in project. – Sss May 27 '14 at 15:10
  • Thanks for the answer but "txtblkShowSelectedValue" and "lines" are declared in other function (which contains"lines.SelectionChanged+=new System.EventHandler(this.UpdateTextBlock);") so how to make both of them in the scope of UpdateTextBlock(object sender, SelectionChangedEventArgs e){} ? (Th eproblem is i cannot declare txtblkShowSelectedValue and lines globally ) – Sss May 27 '14 at 15:13
  • you just need to declare textblockselectedvalue globaly... if you can't, declare an other textblock. Can i see your all code – angel May 27 '14 at 15:16
  • it gives error on line The name 'lines' does not exist in the current context corresponding to this.txtblkShowSelectedValue.Text=this.lines[(sender as Listbox).SelectedIndex].ToString(); – Sss May 27 '14 at 15:21
  • could you please let me know if you understand my current situation ? I guess this way is not possible. Am i right ? Do you anyotherway to do so such that i can impement the selectedItem in the same function (i mean GenerateList() function)? (the reason for this boundation is this GenerateList() function must be reusable and GUI corresponding to is displayed for each time this function is called). Do you know any other way to do so or are you still able to do so using the same way? – Sss May 27 '14 at 15:30
  • oK I AM TRYING TO IMPLEMENT IT..Thnaks – Sss May 28 '14 at 07:20
  • Error 1 Method name expected corresponding to line :lines.SelectionChanged += new SelectionChangedEventHandler(List_SelectionChanged(lines, txtblk1ShowStatus)); and this is red underlined "List_SelectionChanged(lines, txtblk1ShowStatus)" – Sss May 28 '14 at 07:28
  • Error 1 Cannot implicitly convert type 'void' to 'System.Windows.Controls.SelectionChangedEventHandler' corresponding += List_SelectionChanged(lines, txtblk1ShowStatus); – Sss May 28 '14 at 08:10
  • your function which returning Grid, and which have items as parameter (GeneralList), you call it ? – angel May 28 '14 at 08:17
  • what is the name of the grid that you give as parameter, because this grid will have achildren which is the listBox... `Grid GenerateList(ViewModel.XmlParameterClasses.Parameter param, int LoopCount, Grid g)` i need g's name – angel May 28 '14 at 08:21
  • please seemy answer . I have done it. butthanks for trying to help me. – Sss May 28 '14 at 08:24
1

This could be streamlined, but should work as a quick 'n dirty example of one way to solve the problem...

    void lb_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        Grid g = null;
        ListBox lb = sender as ListBox;
        if (lb != null && lb.SelectedIndex >= 0)
        {

            // Find the top-level grid
            var parent = VisualTreeHelper.GetParent(lb);
            while (parent != null)
            {
                if (parent.GetType() == typeof(Grid))
                {
                    if ((parent as Grid).Name.Equals("LayoutRoot"))
                    {
                        g = (Grid)parent;
                        break;
                    }
                }
                parent = VisualTreeHelper.GetParent(parent);
            }
            // Found the LayoutRoot, find the textblock
            if (g != null)
            {
                for (int i = 0; i < g.Children.Count; i++)
                {
                    var child = VisualTreeHelper.GetChild(g, i);
                    if (child is TextBlock)
                    {
                        (child as TextBlock).Text = (string)lb.SelectedItem;
                        break;
                    }
                }
            }
        }
    }

You could also name your textblock and search for that (as I did for "LayoutRoot").
Obviously, this code assumes the textblock is a child of the top-level Grid. Implementing a recursive search wouldn't be difficult.

Number8
  • 12,322
  • 10
  • 44
  • 69
  • See here for a generic version of a FindChild() function: http://stackoverflow.com/a/1759923/107037 – Number8 May 27 '14 at 18:26
  • thanks for this big code but honestly i am still not able to understand what it do and how it do ? Could you please explain a bit that using this code how i would be able to have the scope of "txtblk1ShowStatus" and "lines" in this function call (I mean lb_SelectionChanged() from my GenerateList(..) function) ? Would be a great help . – Sss May 27 '14 at 19:55
  • 1
    A variable (such as txtblk1ShowStatus) local to one function can't be visible in another function. That's why my code searches the visual tree to find the TextBlock control. Note, too, that in your code txtblk1ShowStatus is never added to the visual tree, so it won't ever be visible. Similarly, you don't need to refer to the 'lines' listbox control by variable name -- it is passed in to the SelectionChanged handler as 'sender'. – Number8 May 27 '14 at 20:40
  • As for getting the value of the selected item, this is just *dumb*: `lines[(sender as ListBox).SelectedIndex]`. The value you are trying to get is `(sender as ListBox).SelectedItem`. – Number8 May 27 '14 at 20:42
  • thanks for detailed explanation but how will my function GenerateList(..) will look like ? (from where i am going to call lb_SelectionChanged(..) function ) – Sss May 27 '14 at 21:02
  • please seemy answer . I have done it. butthanks for trying to help me. – Sss May 28 '14 at 08:24
1

Afteralli solved the problem like this:

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

in my function GenerateList(..)

Sss
  • 1,519
  • 8
  • 37
  • 67