使用 caliburn.micro 时在表单中查找控件的最佳方法是什么?

发布于 2024-10-09 21:05:50 字数 80 浏览 0 评论 0原文

使用 Caliburn.Micro 时,有没有一种好方法可以从 ViewModel 中查找表单中的命名控件?国际奥委会是否提供任何获取表格的方法?

Is there a good way to find a named control in a form from the ViewModel when using Caliburn.Micro? Does the IoC provide any way to get at the form?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

左岸枫 2024-10-16 21:05:50

您可以在视图模型(Screen 类型的一部分)中重写 AttachViewOnViewLoaded,Caliburn.Micro 将视图传递给该模型,例如一个按钮控制:

public override void AttachView(object view, object context)
{
  base.AttachView(view, context);

  var frameworkElement = view as FrameworkElement;

  if (frameworkElement == null)
  {
    return;
  }

  var button = frameworkElement.FindName("myButton") as Button;

  if (button == null)
  {
    return;
  }

  // access button control here
}

You can override AttachView or OnViewLoaded in your view model (part of the Screen type), which Caliburn.Micro passes the view to, e.g for a button control:

public override void AttachView(object view, object context)
{
  base.AttachView(view, context);

  var frameworkElement = view as FrameworkElement;

  if (frameworkElement == null)
  {
    return;
  }

  var button = frameworkElement.FindName("myButton") as Button;

  if (button == null)
  {
    return;
  }

  // access button control here
}
海螺姑娘 2024-10-16 21:05:50

您也可以从视图模型调用 GetView()。然后搜索该控件或按名称访问它。

You can call GetView() from the view model also. Then search for the control or access it by name.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文