[英]Dynamically Add listbox item to existing listbox item
I am new in WPF and MVVM. 我是WPF和MVVM的新手。 I have two list List A and List B.First List contains many items and second list contains few item.
我有两个列表A和列表B.第一个列表包含许多项目,第二个列表包含少量项目。 Every item in list A execute sequentially.First item will send command to printer and will get response from printer,if response matches then it will move to next.
列表A中的每个项目都按顺序执行。第一个项目将向打印机发送命令,并从打印机获得响应,如果响应匹配,它将移至下一个项目。
Item form List A send one command or Multiple command. 项目列表列表A发送一个命令或多个命令。
So,now i want to check whether item send one command or multiple command.If it is sending multiple command then i want to display all item of from B list below to respective A's list and data binding for that. 因此,现在我要检查项目是发送一个命令还是多个命令。如果要发送多个命令,那么我要显示从下面的B列表到相应的A的列表的所有项目以及该数据的绑定。
For Single command my code is working fine 对于单命令,我的代码工作正常
Note:List B varies from item to item. 注意:列表B因项目而异。
Following are the properties i used in my code: 以下是我在代码中使用的属性:
private bool isMultiCommand;
public bool IsMultiCommand
{
get { return isMultiCommand; }
set { SetProperty(ref isMultiCommand, value)};
}
public List<TestItem> MultipleCommandTestItemsList { get; set; }
public string TestItemName { get; set; }
private List<TestItem> testItemsList;
public List<TestItem> TestItemsList
{
get { return testItemsList; }
set { SetProperty(ref testItemsList, value); }
}
To update collection and it's items you should use ObservableCollection<TestItem>
instead of List<TestItem>
. 要更新集合及其项目,应使用
ObservableCollection<TestItem>
而不是List<TestItem>
。 TestItem
should also implement INotifyPropertyChanged
to notify when it changes. TestItem
还应该实现INotifyPropertyChanged
以在更改时进行通知。
XAML Design: XAML设计:
<Grid>
<ListBox x:Name="Listbox1" HorizontalAlignment="Left" Height="100" VerticalAlignment="Top" Width="100"/>
<Button x:Name="Add" Content="ADD" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="105,10,0,0" Click="Add_Click"/>
<Button Content="REMOVE" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="105,45,0,0" Click="Remove_Click"/>
<Label Content="Add Listitem" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="105,74,0,0"/>
<TextBox x:Name="textbox1" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" VerticalAlignment="Top" Width="84" Margin="105,100,0,0"/>
</Grid>
XAML Design.cs XAML Design.cs
Add Namespace: using System.Collections.ObjectModel;
添加命名空间:
using System.Collections.ObjectModel;
private ObservableCollection<string> listitem;
public Window5()
{
InitializeComponent();
listitem = new ObservableCollection<string> { "ListItem 1", "ListItem 2" };
Listbox1.ItemsSource = listitem;
}
private void Add_Click(object sender, RoutedEventArgs e)
{
listitem.Insert(listitem.Count, textbox1.Text);
textbox1.Clear();
}
private void Remove_Click(object sender, RoutedEventArgs e)
{
int index = Listbox1.SelectedIndex;
listitem.RemoveAt(Listbox1.SelectedIndex);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.