WPF를 사용하여 조직에서 응용 프로그램에 다른 데이터 조각을 입력 할 수있게하는 응용 프로그램을 만듭니다.이 작업을 수행 할 수있는 탭 컨트롤이 있습니다.선택한 항목 ViewModel에서 EF를 사용하여 업데이트
다른보기에서 필자는 사용자가 어떤 데이터를 데이터베이스에 삽입했는지 보여주는 일련의 다른 데이터 표를 가지고 있습니다. 단추를 포함하여 원하는 데이터를 추가, 업데이트 또는 삭제할 수 있습니다.
내 질문에 이르게합니다. 현재 데이터를 쉽게 삭제하고 추가 할 수 있습니다. 그런 다음 selected
항목을 업데이트하려고 시도 할 때 문제가 발생합니다. 업데이트하지 않으므로 null reference exception
이됩니다.
속성 속성을 프로그래밍 방식으로 설정하면 잘 업데이트됩니다. 그렇게. public int _OrganisationTypeDetailID = 17;
public int _OrganisationTypeID = 1;
그러나 사용자가 직접 선택하고 필요한 데이터를 업데이트 할 수 있기를 원하는 경우에는이 기능이 필요하지 않습니다.
다음은 내 문제를 해결하는 데 도움이되는 코드입니다.
모델보기;
public void UpdateOrganisationTypeDetail(OrganisationTypeDetail orgTypeDetail)
{
using (DBEntities context = new DBEntities())
{
var orgTD = context.OrganisationTypeDetails.Where(otd => otd.OrganisationTypeDetailID == SelectedType.OrganisationTypeDetailID).FirstOrDefault();
if (orgTD != null)
{
orgTD.Title = Title;
orgTD.FirstName = FirstName;
orgTD.Surname = Surname;
orgTD.Position = Position;
orgTD.DateOfBirth = DateOfBirth;
orgTD.Address = Address;
orgTD.Country = Country;
orgTD.Postcode = Postcode;
orgTD.PhoneNumber = PhoneNumber;
orgTD.MobileNumber = MobileNumber;
orgTD.FaxNumber = FaxNumber;
orgTD.Email = Email;
orgTD.NINumber = NINumber;
//context.OrganisationTypeDetails.Attach(orgTD);
context.OrganisationTypeDetails.ApplyCurrentValues(orgTD);
context.SaveChanges();
MessageBox.Show("Updated Organisation Type Details");
}
else
{
MessageBox.Show("Unable to update selected 'Type'.");
}
}
private OrganisationTypeDetail _SelectedType;
public OrganisationTypeDetail SelectedType
{
get
{
return _SelectedType;
}
set
{
if (_SelectedType == value)
return;
_SelectedType = value;
OnPropertyChanged("SelectedType");
}
}
public List<OrganisationTypeDetail> GetOrganisationTypeDetail //Loads data
{
get
{
using (DBEntities context = new DBEntities())
{
var query = from e in context.OrganisationTypeDetails
select e;
return query.ToList<OrganisationTypeDetail>();
}
}
}
private ICommand showUpdateCommand;
public ICommand ShowUpdateCommand //Update command
{
get
{
if (showUpdateCommand == null)
{
showUpdateCommand = new RelayCommand(this.UpdateFormExecute, this.UpdateFormCanExecute); //i => this.UpdateOrganisationTypeDetail()
}
return showUpdateCommand;
}
}
코드 숨김;
private void btnUpdateOrgTypeDetail_Click(object sender, RoutedEventArgs e)
{
OrganisationTypeDetail selected = dgOrgTypeDetail.SelectedItem as OrganisationTypeDetail;
OrganisationTypeDetailViewModel org = new OrganisationTypeDetailViewModel();
if (selected == null)
MessageBox.Show("You must select a 'Type' before updating.");
else
{
OrganisationTypeDetailUpdateView update = new OrganisationTypeDetailUpdateView();
update.ShowDialog();
org.UpdateOrganisationTypeDetail(selected);
Page_Loaded(null, null);
}
}
xaml;
<DataGrid Name="dgOrgTypeDetail" Height="145" Width="555"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding GetOrganisationTypeDetail}"
SelectedItem="{Binding SelectedType, Mode=TwoWay}">
희망 사항을 해결할 수 있기를 바랍니다.
답장을 보내 주셔서 감사합니다. 글쎄, 그냥 MVVM을 사용하는 경우 - 사용자가 업데이트를 입력 할 수있는 새 창 팝업을 어떻게 만듭니 까? –
이 작업을 수행하는 가장 좋은 방법은 IDialogService 인터페이스를 사용하는 것입니다. 다음은 코드 프로젝트를 설명하는 좋은 기사입니다. http://www.codeproject.com/Articles/36745/Showing-Dialogs-When-Using-the-MVVM-Pattern –
당신이 만족한다면 내 대답 - 좋은 녹색 진드기를 줘! –