2

I have a listview and want to dynamicly add items into it using some kind of control. For example enter image description here

Providing that "pack1" "pack2" are ListViewItems I want to have additional ( red square) control which is always visible and adds new "pack" item on click. So the task is quite standard. How is it usually done?

UPDATE CompositeCollection seems to be a nice solution.

Community
  • 1
  • 1
SkySurfer
  • 517
  • 3
  • 15
  • What have you implemented so far for the Pack1 and Pack2 items? Show us some codes so that we could then see where we could add some ideas for you to understand what you're trying to accomplish. – Batuta Mar 01 '16 at 15:36
  • I've added some code, but its just a sketch. In this code its listview inside listview but it does not matter. The main quastion was is there any standard way to do that? – SkySurfer Mar 01 '16 at 15:48
  • I think this code only complicates the questionbecause there is listview inside listview. Real question can be addressed even to default listview whithout any styles. – SkySurfer Mar 01 '16 at 15:51

1 Answers1

1

I have made a very simple layout with a listview. I add a button to the listview items and listen to the click to insert a new object before the button.

public MainWindow()
    {
        InitializeComponent();
        listViewtst.Items.Add("test");
        listViewtst.Items.Add("test2");
        Button btn = new Button();
        btn.Content = "+";
        btn.Click += Btn_Click;
        listViewtst.Items.Add(btn);
    }

    private void Btn_Click(object sender, RoutedEventArgs e)
    {
        listViewtst.Items.Insert(listViewtst.Items.Count - 1, "added by the btn");

    }

Not sure if it is the best solution but this is how i would do it if no one showed me a better way :)


Other solution:

Code behind

    /// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    private ObservableCollection<MyObject> _windows = new ObservableCollection<MyObject>();

    public MainWindow()
    {
        InitializeComponent();
        Windows.Add(new MyObject { Title = "Collection Item 1" });
        Windows.Add(new MyObject { Title = "Collection Item 2" });
    }

    private void Btn_Click(object sender, RoutedEventArgs e)
    {


    }

    public ObservableCollection<MyObject> Windows
    {
        get { return _windows; }
        set { _windows = value; }
    }

    private void button_Click(object sender, RoutedEventArgs e)
    {
        Windows.Add(new MyObject { Title = "From Btn" });
    }
}

public class MyObject
{
    public string Title { get; set; }
}

Xaml:

<Window x:Class="Move_To_Prd_Dev.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="246" Width="325" Name="UI">
<Window.Resources>
    <CollectionViewSource Source="{Binding ElementName=UI, Path=Windows}" x:Key="YourMenuItems"/>
</Window.Resources>
<Grid DataContext="{Binding ElementName=UI}">
    <ListBox x:Name="listTxt" HorizontalAlignment="Left" Height="163" Margin="10,29,0,0" VerticalAlignment="Top" Width="297">
        <ListBox.ItemsSource>
            <CompositeCollection>
                <CollectionContainer Collection="{Binding Source={StaticResource YourMenuItems}}" />
                <Button x:Name="button" Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Click="button_Click"/>

            </CompositeCollection>
        </ListBox.ItemsSource>
    </ListBox>

</Grid>

This uses CompositeCollection to add the button control to the list (with the advantage that the button is always Last)

Result

Lode Vlaeminck
  • 894
  • 1
  • 9
  • 24
  • I'm trying to use MVVM so my listview is bound to ObservableCollection using ItemsSource property in XAML. If there was any way to combine you approach with data binding that would be an ideal solution – SkySurfer Mar 01 '16 at 16:15
  • 1
    you might want to take a look this then http://stackoverflow.com/questions/14489112/how-do-i-dynamically-bind-and-statically-add-menuitems – Lode Vlaeminck Mar 01 '16 at 16:24
  • 1
    Thanks, that's perfect. – SkySurfer Mar 01 '16 at 16:32