我正在使用EntityFramework 6和MVVMLight,但遇到一个我不确定如何以最佳方式解决的问题。
我有一个EquipmentOverview,其中显示了一个设备列表,我们可以通过使用Add New,Edit和Delete按钮进行管理。现在,添加新项或删除它们很顺利,因为我可以使用.Add()
中的.Remove()
和ObservableCollection<EquipmentViewModel>
,并且由于RaisePropertyChanged();
中的RaisePropertyChanged();
,它会自动更新UI列表。 SelectedEquipment对象。
我的问题在于更新设备。我有这个Save方法,它可以很好地更新数据库条目,但是由于某种原因,我无法获得UI列表来更新修改后的Equipment。最好的方法是什么?
EquipmentManagementViewModel.cs
在TODO:点,我想同时更新ObservableCollection列表和UI。我认为UI应该已经更新,因为我在SelectedEquipment上有一个namespace Manager.ViewModel
{
public class EquipmentManagementViewModel : ViewModelBase
{
// Models
private EquipmentOverviewViewModel _equipmentOverviewViewModel;
public EquipmentViewModel Equipment { get; set; }
public ObservableCollection<CategoryViewModel> Categories { get; set; }
// Commands
public ICommand CancelCommand { get; set; }
public ICommand SaveCommand { get; set; }
public EquipmentManagementViewModel(EquipmentOverviewViewModel e)
{
_equipmentOverviewViewModel = e;
Equipment = _equipmentOverviewViewModel.SelectedEquipment ?? new EquipmentViewModel();
RequestCategories();
InitializeCommands();
}
private void RequestCategories()
{
CategoryRepository c = new CategoryRepository();
var categoryList = c.GetCategories();
Categories = new ObservableCollection<CategoryViewModel>(categoryList);
}
private void InitializeCommands()
{
SaveCommand = new RelayCommand(Save);
CancelCommand = new RelayCommand(Cancel);
}
private void Save()
{
using (var context = new ManagerEntities())
{
if (context.Equipments.Find(Equipment.ToModel().Id) != null)
{
Equipment e = context.Equipments.Find(Equipment.ToModel().Id);
EquipmentViewModel equipmentViewModel = new EquipmentViewModel(e);
equipmentViewModel.SetEquipment(Equipment.ToModel());
// TODO: Update Equipment in _equipmentOverviewViewModel.Equipments
_equipmentOverviewViewModel.SelectedEquipment = Equipment;
}
else
{
context.Equipments.Add(Equipment.ToModel());
_equipmentOverviewViewModel.Equipments.Add(Equipment);
}
context.SaveChanges();
}
_equipmentOverviewViewModel.equipmentManagementWindow.Close();
}
private void Cancel()
{
_equipmentOverviewViewModel.equipmentManagementWindow.Close();
}
}
}
,但是当我更改它时,UI中没有任何更新...
namespace Manager.ViewModel
{
public class EquipmentOverviewViewModel : ViewModelBase
{
// Windows
public EquipmentManagementWindow equipmentManagementWindow;
// Models
public ObservableCollection<EquipmentViewModel> Equipments { get; set; }
private EquipmentViewModel _selectedEquipment;
public EquipmentViewModel SelectedEquipment
{
get { return _selectedEquipment; }
set { _selectedEquipment = value; RaisePropertyChanged(); }
}
// Commands
public ICommand AddEquipmentCommand { get; set; }
public ICommand EditEquipmentCommand { get; set; }
public ICommand DeleteEquipmentCommand { get; set; }
public EquipmentOverviewViewModel()
{
InitializeCommands();
EquipmentRepository equipmentRepository = new EquipmentRepository();
var equipmentList = equipmentRepository.GetEquipments();
Equipments = new ObservableCollection<EquipmentViewModel>(equipmentList);
}
private void InitializeCommands()
{
AddEquipmentCommand = new RelayCommand(AddEquipment);
EditEquipmentCommand = new RelayCommand(EditEquipment, HasSelected);
DeleteEquipmentCommand = new RelayCommand(DeleteEquipment, HasSelected);
}
private void AddEquipment()
{
SelectedEquipment = null;
EditEquipment();
}
private void EditEquipment()
{
equipmentManagementWindow = new EquipmentManagementWindow();
equipmentManagementWindow.Show();
}
private void DeleteEquipment()
{
using (var context = new ManagerEntities())
{
Equipment equipment = SelectedEquipment.ToModel();
context.Equipments.Attach(equipment);
context.Equipments.Remove(equipment);
context.SaveChanges();
}
Equipments.Remove(SelectedEquipment);
}
private bool HasSelected()
{
return SelectedEquipment != null;
}
}
}
EquipmentOverviewViewModel.cs
<Window x:Class="Manager.View.EquipmentOverviewWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Manager.View"
mc:Ignorable="d"
WindowStartupLocation="CenterScreen"
ResizeMode="NoResize"
DataContext="{Binding EquipmentOverview, Source={StaticResource Locator}}"
Title="Manage Equipment" Height="350" Width="550">
<StackPanel>
<Grid Margin="10,10,10,0" Height="250">
<DataGrid AutoGenerateColumns="False" IsReadOnly="True"
SelectedItem="{Binding SelectedEquipment}" ItemsSource="{Binding Equipments}">
<DataGrid.Columns>
<DataGridTextColumn Header="#" Binding="{Binding Id}" />
<DataGridTextColumn Header="Name" Binding="{Binding Name}" Width="160" />
<DataGridTextColumn Header="Intelligence" Binding="{Binding Intelligence}" Width="75" />
<DataGridTextColumn Header="Strength" Binding="{Binding Strength}" Width="60" />
<DataGridTextColumn Header="Agility" Binding="{Binding Agility}" Width="50" />
<DataGridTextColumn Header="Value" Binding="{Binding Value}" Width="50" />
<DataGridTextColumn Header="Category" Binding="{Binding CategoryName}" Width="*" />
</DataGrid.Columns>
</DataGrid>
</Grid>
<Grid Margin="0,0,0,10" Height="45" VerticalAlignment="Center" HorizontalAlignment="Center">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Button Width="100" Height="30" FontSize="16"
Foreground="White" Background="#FF6F9BDA" Content="Add New"
Grid.Column="0" Command="{Binding AddEquipmentCommand}">
</Button>
<Button Width="100" Height="30" FontSize="16"
Foreground="White" Background="#FF6F9BDA" Content="Edit"
Grid.Column="1" Command="{Binding EditEquipmentCommand}">
</Button>
<Button Width="100" Height="30" FontSize="16"
Foreground="White" Background="#FF6F9BDA" Content="Delete"
Grid.Column="2" Command="{Binding DeleteEquipmentCommand}">
</Button>
</Grid>
</StackPanel>
</Window>
,如有必要,还有EquipmentOverviewWindow.xaml
import java.util.Arrays;
public class MyClass {
public static void main(String[] args) {
String[] ArrayInput = new String[] { "The", "Black", "Cat" };
outputInReverse(ArrayInput);
}
public static void outputInReverse(String[] FirstOrder) {
String[] FinalOrder = new String[] {"One", "Two", "Three"};
for (int i = 2; i >= 0; i--) {
for (int j = 0; j <= 2; j++) {
FinalOrder[j] = FirstOrder[i];
System.out.println(FinalOrder[j]);
}
}
System.out.println(Arrays.toString(FinalOrder));
}
}
任何提示将不胜感激。看来我正在忽略某些东西。