ListBox的SelectedIndex中的Dynamic Combobox

时间:2017-06-21 05:55:32

标签: c# wpf listbox margin selectedindex

在WPF应用程序中,我有一个ListBox并绑定了来自Sql的数据。并且在列表框中也有一个堆栈面板。

一旦我双击列表框中的项目,我需要放置/添加动态组合框。

我可以获取ListBox的选定索引。

int seleteditem = lstbxusername.SelectedIndex;

我创建了动态Combobox

  System.Windows.Controls.ComboBox cmb = new ComboBox();
            cmb.Background = Brushes.Green;

使用Stackpanel添加组合框

        newstckpnl.Children.Add(cmb);

我的问题是如何在列表框的选定索引中添加动态组合框。

这是否可能?有帮助。

2 个答案:

答案 0 :(得分:0)

当然可以。您只需首先从ListBoxItem中获取ListBox,如下所示。

var listBox = new ListBox();
var listBoxItem = listBox.SelectedItem as ListBoxItem;
var listBoxItemMargin = listBoxItem.Margin;

目前,您正在为ListBox添加字符串。字符串显然没有边距。要使上述代码有效,您需要将ListBoxItems添加到ListBox,如下所示。

listBox.Items.Add(new ListBoxItem {Content = dr.GetString(1) });

答案 1 :(得分:0)

我做了一件事,它对我有用!!

我获得了列表框的选定索引

int seleteditem = lstbxusername.SelectedIndex;

然后创建了组合框

System.Windows.Controls.ComboBox cmb = new ComboBox();

通过使用组合框对象,我在列表框中选择了索引

 lstbxusername.Items.Insert(seleteditem, cmb);
相关问题