简体   繁体   中英

Dynamically Add listbox item to existing listbox item

I am new in WPF and MVVM. I have two list List A and List B.First List contains many items and second list contains few item. 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.

Item form List A send one command or Multiple command.

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.

For Single command my code is working fine

Note:List B varies from item to item.

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

This is my .xaml code

To update collection and it's items you should use ObservableCollection<TestItem> instead of List<TestItem> . TestItem should also implement INotifyPropertyChanged to notify when it changes.

XAML Design:

<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

Add Namespace: 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);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM