gpt4 book ai didi

C# WPF : ItemsControl with Add-Content Button

转载 作者:太空宇宙 更新时间:2023-11-03 23:18:32 25 4
gpt4 key购买 nike

我想实现一个带有按钮的 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>

关于C# WPF : ItemsControl with Add-Content Button,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36328089/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com