繁体   English   中英

C#WPF:带有添加内容按钮的ItemsControl

[英]C# WPF: ItemsControl with Add-Content Button

我想用一个按钮来实现一个ItemsControl,该按钮可以向另一个ViewModel添加相同的内容。 到目前为止,我有这个代码:

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

该按钮应该始终是控件中的最后一项,它应该只有一个添加按钮。 有没有人有一个好的解决方案? 我可以通过丑陋的变通方法自己做,但我讨厌丑陋的变通办法:)

您需要自定义ItemsControlTemplate ,以便在ItemsPresenter下面添加Button

<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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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