[英]C# WPF: ItemsControl with Add-Content Button
我想用一個按鈕來實現一個ItemsControl,該按鈕可以向另一個ViewModel添加相同的內容。 到目前為止,我有這個代碼:
<ItemsControl ItemsSource="{Binding Items}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Controls:ItemView />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
該按鈕應該始終是控件中的最后一項,它應該只有一個添加按鈕。 有沒有人有一個好的解決方案? 我可以通過丑陋的變通方法自己做,但我討厭丑陋的變通辦法:)
您需要自定義ItemsControl
的Template
,以便在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.