【问题标题】:How to select an item from a ListBox without using SelectionChange event如何在不使用 SelectionChange 事件的情况下从 ListBox 中选择一个项目
【发布时间】:2014-05-27 14:46:34
【问题描述】:

我是 Silverlight 开发人员,使用 C# 编写代码以从列表中选择一个项目并在附近的 textBlock 中显示所选项目。

我这样做的代码是:

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;

问题是如何选择一个值以及如何在文本块“txtblkShowSelectedValue”中显示它?因为如果我使用 selectionChange 事件,由于当前条件,我无法全局声明 textblock 和 List 变量

编辑:当前情况是:(行(列表)在不同的函数中,所以它不在List_SelectionChanged()函数的范围内)

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

【问题讨论】:

  • 为 Listbox 的 SelectionChanged 事件添加一个函数。并在此函数中写入:this.txtblkShowSelectedValue.Text=this.lines[(sender as Listbox).SelectedIndex] 其中 sender 是函数的一对二参数
  • @angel 你知道只使用 c# 代码的任何方法吗? Otherthen selectiONcHANGED EVENT ?

标签: c# silverlight listbox silverlight-5.0 selecteditem


【解决方案1】:

这可以简化,但应该作为解决问题的一种方法的快速'n脏示例......

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

您也可以为您的文本块命名并搜索它(就像我对“LayoutRoot”所做的那样)。
显然,此代码假定文本块是顶级 Grid 的子项。实现递归搜索并不难。

【讨论】:

  • 在此处查看 FindChild() 函数的通用版本:stackoverflow.com/a/1759923/107037
  • 感谢这个大代码,但老实说我仍然无法理解它的作用以及它是如何做的?您能否解释一下使用此代码我如何能够在此函数调用中拥有“txtblk1ShowStatus”和“lines”的范围(我的意思是来自我的 GenerateList(..) 函数的 lb_SelectionChanged())?会很有帮助。
  • 一个函数的局部变量(例如 txtblk1ShowStatus)在另一个函数中不可见。这就是我的代码搜索可视化树以查找 TextBlock 控件的原因。还要注意,在您的代码中 txtblk1ShowStatus 永远不会添加到可视化树中,因此它永远不会可见。同样,您不需要通过变量名称引用“行”列表框控件——它作为“发送者”传递给 SelectionChanged 处理程序。
  • 至于获取选中项的值,这只是dumblines[(sender as ListBox).SelectedIndex]。您尝试获取的值是(sender as ListBox).SelectedItem
  • 感谢您的详细解释,但我的函数 GenerateList(..) 会是什么样子? (我将从那里调用 lb_SelectionChanged(..) 函数)
【解决方案2】:
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
}

编辑:谢谢,很抱歉迟到了:-)

试试这个:

为函数添加参数,如下:

    lines.SelectionChanged += new SelectionChangedEventHandler(List_SelectionChanged)

更改此函数的参数并将您的文本块设置为:

static void List_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    MessageBox.Show("clist   _SelectionChanged1");
    txtblkShowSelectedValue.Text=this.lines[(sender as Listbox).SelectedIndex].ToString()
}

【讨论】:

  • 你可以加txtblkShowSelectedValue.isReadOnly=True // texblock will not be edited by user, only by the function
  • 我已经完成了,但问题是这个“txtblkShowSelectedValue”在包含对“UpdateTextBlock()”selectionchanged 事件的调用的函数范围内。而且我不能全局执行 TextBlock txtblkShowSelectedValue = new TextBlock();(由于当前情况)所以它有问题
  • 错误1 this.txtblkShowSelectedValue.Text=this.lines[(sender as Listbox).SelectedIndex].ToString();由于项目的当前情况,我无法在全球范围内对其进行贴花。
  • 感谢您的回答,但“txtblkShowSelectedValue”和“lines”是在其他函数中声明的(其中包含“lines.SelectionChanged+=new System.EventHandler(this.UpdateTextBlock);”)所以如何使两者它们在 UpdateTextBlock(object sender, SelectionChangedEventArgs e){} 的范围内吗? (问题是我不能全局声明 txtblkShowSelectedValue 和行)
  • 您只需要全局声明 textblockselectedvalue ... 如果不能,请声明另一个文本块。我可以看看你的所有代码吗
【解决方案3】:

Afteralli 解决了这样的问题:

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

在我的函数GenerateList(..)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多