如何将Addhandler添加到wpf中的stackpanel [英] how to add Addhandler to stackpanel in wpf

查看:131
本文介绍了如何将Addhandler添加到wpf中的stackpanel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个scrollviewer并在其上插入stackpanel并在stackpanel上添加listbox。当我点击该列表框时我想显示所选的值。我的代码是

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

//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() + ".");
        }

推荐答案

我想你想要这个,你的问题不是很清楚。



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() + ".");   
        }


这篇关于如何将Addhandler添加到wpf中的stackpanel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆