Hans Dabi
Hans Dabi

Reputation: 181

C# WPF: ItemsControl with Add-Content Button

I want to implement an ItemsControl with a button that adds the same content with another ViewModel. So far I have this code:

<ItemsControl ItemsSource="{Binding Items}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Controls:ItemView />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

The button should always be the last item in the control and it should have only one add button. Does anyone have a good solution? I could do it on my own with ugly workarounds but I hate ugly workarounds :)

Upvotes: 2

Views: 1906

Answers (1)

Andy
Andy

Reputation: 3743

You need to customize the Template of your ItemsControl in order to add the Button below the ItemsPresenter:

<ItemsControl ItemsSource="{Binding Items}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Controls:ItemView />
        </DataTemplate>
    </ItemsControl.ItemTemplate>

    <ItemsControl.Template>
        <ControlTemplate TargetType="ItemsControl">
            <StackPanel>
                <ItemsPresenter />
                <Button Content="Add Item"  Click="AddItem_Click"/>
            </StackPanel>
        </ControlTemplate>
    </ItemsControl.Template>

</ItemsControl>

Upvotes: 9

Related Questions