MVVM 对话框 wpf 定位
我使用一项服务来管理对话框,该服务运行良好,并将可视化细节与我的视图模型分离。
我的代码(如下)可以轻松地将对话框窗口所有者设置为主窗口,但我真正想做的是将其放置在调用它的图形项(即按钮,无论什么)旁边。
有人有 MVVM 解决方案吗?
干杯,
贝里尔
// helper to create and prep windows in WPF
private Window _createWindow(
string key, object dataContext, bool setOwnerToCurrentMainWindow,
EventHandler<UICompletedEventArgs> completedProc, bool isModal)
{
var win = _registrationService.CreateRegisteredType<Window>(key);
win.DataContext = dataContext;
...
if (setOwnerToCurrentMainWindow)
win.Owner = Application.Current.MainWindow;
...
return win;
}
I use a service to manage dialogs, which works nicely and decouples the visualization details from my view models.
My code (below) can easily set the dialog window owner to the Main Window, but what I would really like to do is position it next to the graphical item (ie, button, whatever) that called it.
Has anyone got an MVVM solution to this?
Cheers,
Berryl
// helper to create and prep windows in WPF
private Window _createWindow(
string key, object dataContext, bool setOwnerToCurrentMainWindow,
EventHandler<UICompletedEventArgs> completedProc, bool isModal)
{
var win = _registrationService.CreateRegisteredType<Window>(key);
win.DataContext = dataContext;
...
if (setOwnerToCurrentMainWindow)
win.Owner = Application.Current.MainWindow;
...
return win;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那么是什么阻止了你呢?据我所知,在 MVVM 场景中完全允许使用附加行为的帮助。
如果您不想完全使用附加行为来尝试此操作...那么
现有图形项目的坐标值
左上角
和大小
可以是< code>OneWayToSource 绑定到父视图的数据上下文(比方说ParentVM
)。如果不是,因为
ActualSize
是只读的,所以通过附加行为
绑定它(在 MVVM 中再次完全允许)。所以
ParentVM 可以保存原始图形项的 Top、Lef、Bottom、Right 坐标。它们可以通过
dataContext
(我猜你可以发送)发送到您的窗口创建调用,我们可以将其绑定到子窗口的Top
和左
。如果我的回复有什么遗漏的地方请批评指正。 :)
Then whats stopping you? As far as I know, taking help of
Attached Behavior
is perfectly allowed in the MVVM scenario.If you dont want to try this using attached behavior totally ... then
The existing graphical item's coordinate values
Top-Left
andSize
can beOneWayToSource
bound to your parent View's data context (lets sayParentVM
).If not because
ActualSize
is ReadOnly so bind it via anAttached Behavior
(again perfectly allowed in MVVM).So the
ParentVM
can hold Top, Lef, Bottom, Right coordinates of the original graphical item. They can be sent across to your Window creation call via thedataContext
(which is something I guess you can send) where we can bind that to the child Window'sTop
andLeft
.Do criticise if there is something a miss in my reply. :)