需要澄清多列列表框 [英] Need a clarification about multicolumn listbox
问题描述
我要将两个文本框中的数据添加到列表框中,我想以水平方式添加数据.假设我添加了两个文本框中的数据,我希望它们以水平方式显示,对于下一个条目,它们应该可以在上一个条目的下面显示.还可以为数据提供标题以显示该数据的含义.(我知道可以使用列表视图来完成,但是我想知道是否可以使用列表框来完成)
名称代码< --- header
山姆060
rob 061
i am adding data from two text boxes into a listbox,i want to add the data in horizontal manner.suppose i add data from two text boxes i want them to be displayed in horizontal manner,and for the next entry they should be displayed below the previous entry.and also can a header be provided for the data to display what that data meant to be.(i know it can be done using list view, but i want to know whether it can be done using listbox)
name code <---header
sam 060
rob 061
推荐答案
执行以下操作:
Do like this :
//Create two classes
Class Person
{
private String name;
public String Name
{
get{return name;}
set{name=value;}
}
private int code;
public int Code
{
get{return code;}
set{code=value;}
}
//ctor
public Person(String name,int code)
{
Name=name;
Code=code;
}
}
Class PersonCollection:ObservableCollection<person>
{
}
//Go into the code-behind of your application
// Add this in your constructor
public Window1
{
InitializeComponent();
PersonCollection perColl=new PersonCollection();
listBox.ItemsSource=perColl;
}
//In the Add button event handler(that adds the value in ListBox), do this
perColl.Add(new Person(txtName.Text,int.Parse(txtCode.Text));</person>
移至您的XAML:
Move over to your XAML :
<listbox name="listBox">
<listbox.itemtemplate>
<datatemplate>
<stackpanel orientation="Horizontal">
<textblock text="{Binding" path="Name}/">
<textblock text="{Binding" path="Code}/">
</textblock></textblock></stackpanel></datatemplate>
</listbox.itemtemplate>
</listbox>
DataTemplate上方的Stackpanel将以水平方式保留名称和代码.
希望对您有所帮助!
The Stackpanel above in your DataTemplate will keep the name and code in a horizontal way.
Hope it helped!
这篇关于需要澄清多列列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!