Click here to Skip to main content
15,886,512 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have created one scrollviewer and insert stackpanel on it and add listbox on stackpanel .When I click that listbox i want to display the selected value.my code is
C#
//XAML

  <ScrollViewer x:Name="ScrollViewer" Style="{StaticResource ScrollViewerStyle}" VerticalAlignment="Bottom" CanContentScroll="True" Cursor="Arrow" ForceCursor="False">

            <ItemsControl x:Name="itemsControl" VerticalAlignment="Bottom">
                <ItemsControl.ItemsPanel>

                    <ItemsPanelTemplate>
                        <!-- Custom Panel-->

                        <StackPanel Orientation="Horizontal"   removed="AliceBlue"  >
                           

                      </StackPanel>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
               
            </ItemsControl>
        </ScrollViewer>

  private StackPanel createvideostack()
        {
            StackPanel isv = new StackPanel();
            isv.Orientation = Orientation.Vertical;
            met = new MediaElement();
            met.Width = 150;
            met.Height = 150;
            isv.Children.Add(met);
            StackPanel vid = new StackPanel();
            Vlist = new ListBox();
            Vlist.Height = 150;
            Vlist.Width = 150;
            Vlist.Items.Clear();
          
            DirectoryInfo DirInfo = new DirectoryInfo(pathsv);
            string[] array1 = Directory.GetFiles(pathsv);
            foreach (string finfo in Directory.GetFiles(pathsv))
          
            {
                Vlist.Items.Add(System.IO.Path.GetFileName(finfo));
            }
            Vlist.SelectionChanged += new SelectionChangedEventHandler(OnSelectionChanged);
            isv.Children.Add(Vlist);
            return isv;
        }
        }
  void OnSelectionChanged(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("You have selected " + ((System.Windows.Controls.ListBox)(sender)).SelectedItems[0].ToString() + ".");
        }
Posted
Updated 1-Jul-14 18:49pm
v3
Comments
Sergey Alexandrovich Kryukov 30-Jun-14 14:34pm    
The question makes no sense. If you added, a list box in code behind, you can add an event handler as well. This is not related to XAML. So, what's the problem? (Especially after you mentioned AddHandler yourself...)
—SA
Maheswari_Pandian 2-Jul-14 0:57am    
I have edit my question and insert my coding. pls refer and help me,Scrollviewer->stackpanel->listbox events?

1 solution

I Suppose you wanted this, your question is not very clear.

private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            CreateStackPanel();
        }
        private void CreateStackPanel()
        {
            // Create a StackPanel
            StackPanel MyStackPanel = new StackPanel();
            MyStackPanel.Orientation = Orientation.Horizontal;

            //Add Listbox and add Items
            ListBox lstBox = new ListBox();
            lstBox.Items.Add("item1");
            lstBox.Items.Add("item2");
            lstBox.Items.Add("item3");
            lstBox.Items.Add("item4");
            lstBox.Items.Add("item5");
            //Adding Handler to Listbox
            lstBox.SelectionChanged += new SelectionChangedEventHandler(OnSelectionChanged);
            MyStackPanel.Children.Add(lstBox);

            // Display StackPanel
            RootWindow.Content = MyStackPanel;
        }
       
        void OnSelectionChanged(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("You have selected " + ((System.Windows.Controls.ListBox)(sender)).SelectedItems[0].ToString() + ".");   
        }
 
Share this answer
 
Comments
Maheswari_Pandian 2-Jul-14 0:10am    
Thank You very much.It works

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900