如何通过单击没有任何代码隐藏的按钮将列表框中的项目添加到列表中? [英] How can I add items from a listbox to a list by clicking a button without any codebehind?
问题描述
我是MVVM的新手,也是WPF的新手.事实上,我几个月前才开始编程. MVVM确实使我对绑定概念有所了解,并且我已经尝试了几天,只是简单地制作了一个应用程序,该应用程序允许您从listbx中选择一个项目,并且当您单击添加按钮时,所选项目应该是保存在新列表中.第二个列表框显示最新添加的项目,您可以选择一个项目并使用另一个按钮将其删除.通常,我会参加click事件并使用很少的方法来装饰我的代码隐藏,但是我真的很想学习如何通过使用绑定而不使用任何代码隐藏来完成所有这些工作. 如果有任何帮助,我将非常高兴,并且请记住,我对此并不陌生,我真的想让它尽可能简单:) 丹妮拉(Daniela)致以亲切的问候
I am new to MVVM, and also fairly new to WPF. As a matter of fact I started programming just a few months ago. MVVM is really dng my head in with the binding concept, and I have been trying for days now to just simply make an application that allows you to select an item from a listbx, and when you click on the add button the selected item should be saved in a new list. The second listbox displays the latest items added, and you can select an item and delete it by using another button. ususally I would go for the click event and decorate my codebehind with pretty little methods, but I really want to learn how to do all this by using bindings and no codebehind. I would be extremly happy for any help, and please remember that I am new to this and I really want to keep it as simple as possible :) with kind regards Daniela
<WrapPanel HorizontalAlignment="Center" Margin=" 10">
<ListBox x:Name="Firstbox"
Width="100"
ItemsSource="{Binding FoodList}"
DisplayMemberPath="Name" >
</ListBox>
<Button Margin="10 >Select</Button>
<ListBox Width="100"></ListBox>
私人列表_foodList;
private List _foodList;
public List<FoodItem> FoodList
{
get { return _foodList; }
set { _foodList = value; }
}
private List<FoodItem> _newFoodList;
public List<FoodItem> NewFoodList
{
get { return _newFoodList; }
set { _newFoodList = value; }
}
public MainViewModel()
{
InitializeCommands();
GetFood();
}
private void GetFood()
{
FoodList = new List<FoodItem>()
{
new FoodItem() {Name="Applepie"},
new FoodItem() {Name="Scones"}
};
}
推荐答案
- 首先,您需要将
List
替换为ObservableCollection
,以便UI可以检测到何时添加了新项目. -
向您的ViewModel添加
SelectedItem
属性:- first, you need to replace the
List
s withObservableCollection
s, so that the UI can detect when new items are added. Add a
SelectedItem
property to your ViewModel:private FoodItem _selectedItem; public FoodItem SelectedItem { get { return _selectedItem;} set { _selectedItem = value; OnPropertyChanged("SelectedItem"); } }
-
将第一个
ListBox
的SelectedItem
属性绑定到该属性: bind the
SelectedItem
property of the 1stListBox
to this property:<ListBox Width=" 100" x:Name="Firstbox" ItemsSource="{Binding FoodList}" DisplayMemberPath="Name" SelectedItem="{Binding SelectedItem}" />
-
将您的第二个
ListBox
绑定到NewFoodList
属性 bind your 2nd
ListBox
to theNewFoodList
property在ViewModel中创建命令:
create a command in your ViewModel:
private DelegateCommand _addItemCommand; public ICommand AddItemCommand { get { if (_addItemCommand == null) { _addItemCommand = new DelegateCommand(AddItem); } return _addItemCommand; } } void AddItem() { if (SelectedItem != null) NewFoodList.Add(SelectedItem); }
-
最后,将按钮的
Command
属性绑定到AddItemCommand
属性: And finally, bind the button's
Command
property to theAddItemCommand
property:<Button Margin="10" Command="{Binding AddItemCommand}" >Select</Button>
这篇关于如何通过单击没有任何代码隐藏的按钮将列表框中的项目添加到列表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- first, you need to replace the