我想知道是否可以将列表框绑定到多个文本框。 我的意思是如果我点击addButton,我会在同一行(水平)同时添加5个文本框的内容。 我知道不可能有多个stackpanel或datatemplate所以我想知道是否可能 TY
答案 0 :(得分:2)
您可以将新文本框放在DataTemplate中,然后将新的空项添加到您要绑定的集合中:
<ListBox x:Name="myLb">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBox Width="85" Text="{Binding Field1}" />
<TextBox Width="85" Text="{Binding Field2}" />
<TextBox Width="85" Text="{Binding Field3}" />
<TextBox Width="85" Text="{Binding Field4}" />
<TextBox Width="85" Text="{Binding Field5}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
使用上面的代码后面跟着以下代码。
public partial class MainPage : PhoneApplicationPage
{
private ObservableCollection<MyListViewModel> myVM;
public MainPage()
{
InitializeComponent();
Loaded += MainPage_Loaded;
}
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
myVM = new ObservableCollection<MyListViewModel>
{
new MyListViewModel
{
Field1 = "A1",
Field2 = "A2",
Field3 = "A3",
Field4 = "A4",
Field5 = "A5"
},
new MyListViewModel
{
Field1 = "B1",
Field2 = "B2",
Field3 = "B3",
Field4 = "B4",
Field5 = "B5"
}
};
myLb.ItemsSource = myVM;
}
private void ApplicationBarIconButton_Click(object sender, EventArgs e)
{
myVM.Add(new MyListViewModel());
}
}
public class MyListViewModel
{
public string Field1 { get; set; }
public string Field2 { get; set; }
public string Field3 { get; set; }
public string Field4 { get; set; }
public string Field5 { get; set; }
}