gpt4 book ai didi

c# - 在 MVVM 中的窗口之间传递 DataContext

转载 作者:行者123 更新时间:2023-11-30 13:05:46 25 4
gpt4 key购买 nike

在我的主窗口onClick上

AddNoticeAboutWrongCity addNoticeAboutWrongCity = new AddNoticeAboutWrongCity();
addNoticeAboutWrongCity.DataContext = ((VerificationViewModule)this.DataContext).WrongCityNotice;
addNoticeAboutWrongCity.ShowDialog();

弹出窗口有很多文本框和两个按钮

删除对象:

this.DataContext = null;

第二个选项“Save edited notice”不可用,因为主窗口上用户情感数据上下文的每次更改,这是设计部门的要求:)

我不知道为什么第一个选项(它的“实现”不起作用。

第二种解释:

在 ParentWindow 上我有通知列表,我可以单击 EditSelectedNotice。

在 EditNoticeWindow 上我可以编辑通知或删除通知。

Editinig 有效(关闭 EditNoticeWindow 后,我在 ParentWindow 上看到更改的通知),但删除没有(通知仍在集合中 - 在控件上和 this.DataContext 中)

我的 View 模型:

class VerificationViewModule
{
public ObservableCollection<ReporterNotice> ReporterNotices { get; set; }

public ReporterNotice OtherNotice
{
get
{
return ReporterNotices.Where(n => n.Type == ReporterNoticeType.Other).FirstOrDefault();
}
}
public ReporterNotice DuplicateNotice
{
get
{
return ReporterNotices.Where(n => n.Type == ReporterNoticeType.Duplicate).FirstOrDefault();
}
}
public ReporterNotice WrongCityNotice
{
get
{
return ReporterNotices.Where(n => n.Type == ReporterNoticeType.WrongCity).FirstOrDefault();
}
set { if(value==null)
{
ReporterNotices.Remove(ReporterNotices.Where(n => n.Type == ReporterNoticeType.WrongCity).First());
}
else
{
if (ReporterNotices.Where(n => n.Type == ReporterNoticeType.WrongCity).FirstOrDefault()==null)//there is always only max one instance of this type of notice
{
ReporterNotices.Add(value);
}
else
{
var c = ReporterNotices.Where(n => n.Type == ReporterNoticeType.WrongCity).First();
c = value;

}
}}
}

public VerificationViewModule()
{
ObservableCollection<ReporterNotice> loadedReporterNotices = new ObservableCollection<ReporterNotice>();
loadedReporterNotices.Add(new ReporterNotice() { Content = "Dublic", Type = ReporterNoticeType.WrongCity });
loadedReporterNotices.Add(new ReporterNotice() { Content = "Hilton", Type = ReporterNoticeType.Duplicate });
loadedReporterNotices.Add(new ReporterNotice() { Content = "Another notice", Type = ReporterNoticeType.Other });
ReporterNotices = loadedReporterNotices;
}


}

最佳答案

您可以尝试以下操作。实现调解器以显示窗口,并确保为主窗口和编辑窗口都使用 DataContext 的 View 模型。重要的是要告诉主视图模型对象正在被删除。这是通过回调和路由完成的,通过 EditNoticeViewModel 上的命令

    //This viewmodel is on the main windows datacontext
public class ParentViewModel
{
private readonly IWindowMediator _mediator;
public ParentViewModel(IWindowMediator mediator)
{
_mediator = mediator;
}

public ObservableCollection<Notice> Notices { get; private set; } //bound to list in xaml

public void OpenNotice(Notice notice)
{
//open the window using the Mediator pattern rather than a new window directly

_mediator.Open(new EditNoticeViewModel(notice, DeleteNotice));
}

private void DeleteNotice(Notice notice)
{
//This will remove it from the main window list
Notices.Remove(notice);
}
}

//view model for EditNoticeWindow
public class EditNoticeViewModel
{
public EditNoticeViewModel(Action<Notice> deleteCallback, Notice notice)
{
Model = notice;

DeleteCommand = new DelegateCommand((a) => deleteCallback(Model));
}

//Bind in xaml to the Command of a button
DelegateCommand DeleteCommand { get; private set; }

//bound to the controls in the xaml.
public Notice Model { get; private set; }
}

//This is a basic interface, you can elaborate as needed
//but it handles the opening of windows. Attach the view model
//to the data context of the window.
public interface IWindowMediator
{
void Open<T>(T viewModel);
}

根据实现,您可能希望在按下删除按钮时关闭 View 。您可以通过实现类似 here 中描述的内容来做到这一点关于 WorkspaceViewModel

关于c# - 在 MVVM 中的窗口之间传递 DataContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3179582/

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