- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个表单,我想在发生某些用户操作时将焦点设置到文本框。我知道 MVVM 的做事方式是绑定(bind)到 VM 属性,但是 TextBox 没有允许这种情况发生的属性。从 VM 设置焦点的最佳方法是什么?
最佳答案
我已经创建了一个 IResult 实现,它可以很好地实现这一目标。您可以从 IResult 的 ActionExecutionContext 获取 View ,然后您可以搜索(我按名称搜索)您想要聚焦的控件。
public class GiveFocusByName : ResultBase
{
public GiveFocusByName(string controlToFocus)
{
_controlToFocus = controlToFocus;
}
private string _controlToFocus;
public override void Execute(ActionExecutionContext context)
{
var view = context.View as UserControl;
// add support for further controls here
List<Control> editableControls =
view.GetChildrenByType<Control>(c => c is CheckBox ||
c is TextBox ||
c is Button);
var control = editableControls.SingleOrDefault(c =>
c.Name == _controlToFocus);
if (control != null)
control.Dispatcher.BeginInvoke(() =>
{
control.Focus();
var textBox = control as TextBox;
if (textBox != null)
textBox.Select(textBox.Text.Length, 0);
});
RaiseCompletedEvent();
}
}
当 view
是 ChildWindow
时,我省略了一些额外的代码以从 context
获取 view
如果您需要,我可以提供。
此外,GetChildrenByType 是一种扩展方法,这里是许多可用的实现之一:
public static List<T> GetChildrenByType<T>(this UIElement element,
Func<T, bool> condition) where T : UIElement
{
List<T> results = new List<T>();
GetChildrenByType<T>(element, condition, results);
return results;
}
private static void GetChildrenByType<T>(UIElement element,
Func<T, bool> condition, List<T> results) where T : UIElement
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(element); i++)
{
UIElement child = VisualTreeHelper.GetChild(element, i) as UIElement;
if (child != null)
{
T t = child as T;
if (t != null)
{
if (condition == null)
results.Add(t);
else if (condition(t))
results.Add(t);
}
GetChildrenByType<T>(child, condition, results);
}
}
}
您的操作将类似于以下内容(以 Caliburn.Micro ActionMessage 样式调用)。
public IEnumerable<IResult> MyAction()
{
// do whatever
yield return new GiveFocusByName("NameOfControlToFocus");
}
关于c# - 如何使用 Caliburn.Micro MVVM 将焦点设置到控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4571328/
最近我看到this详细说明 Caliburn 有多棒的文档(实际上并没有将它与微框架进行比较,这就足够了)。我在 Caliburn 工作了一年多,对它了解不多。 所以也许有人可以解释以下内容(其中一些
我有以下 ViewModel,我正在使用 Caliburn Micro。 IWindowManager 实例已正确解析,并且所有代码都可以正常工作。如 TODO 注释所示,我需要获取对当前窗口的引用,
所以我在 Caliburn.Micro 上摆弄了一下,突然发现了一些有趣的东西。 我有一个名为 Maximum 的 ViewModel 属性,类型为 int,通过命名约定与 CM 自动绑定(bind)
如果我的 View 中有一个名为 Save 的按钮,那么我可以将 Save 属性添加到我的 ViewModel,Caliburn.Micro 会自动将它绑定(bind)到我的按钮的内容。例如: pub
我一直在研究开发一个新的 MVVM WPF 桌面应用程序,并已将 Caliburn 和 Caliburn.Micro 列入候选名单。 我特别喜欢您在 Caliburn 中获得的自动绑定(bind)属性
当我使用绑定(bind)约定时在caliburn中,并将内容控件命名为“CurrentPresenter” 框架自动绑定(bind)到虚拟机并定位相关 View 。 如果我手动执行此绑定(bind),
我将 ViewModels 绑定(bind)到 ContentControls 并让 Caliburn 负责创建和绑定(bind) View 。但是,我想根据我绑定(bind)的 ContentCon
我从 Caliburn.micro 开始,我有点困惑。假设我有一个带有 2 个 Pane 的 UI,例如(这是一个假示例)CustomersView 和 CustomerView,以及 2 个相应的
我一直在尝试将 Caliburn.Micro MVVM 框架集成到我处于中间位置的 C# WPF 项目中。 我目前只有三个 View 模型: ShellViewModel -(带有 ContentCo
Caliburn.Micro 是否有与 Catel's 类似的功能? [ExposeAttribute] ? 有没有其他方法可以减轻 Caliburn.Micro 中传递属性的工作? (即模型中的属性
我想弄清楚如何将文本添加到列表框的底部并显示它。在带有代码的 WPF 中,我会捕获 ScrollViewer 并对其进行操作,但我无法弄清楚如何使用 Caliburn 来做到这一点... 最佳答案 您
我这样做: 问题是,这同时是主窗口上定义的主导体,我用它控制通过其他窗口的导航,所以我不能从 MetroWindow 继承,至少尝试更改 ViewModel 中的标题: public class S
我试图弄清楚如何成功地让 Caliburn Micro 在 Windows Phone 8.1 应用程序中从一个页面导航到另一个页面。 我的第一页加载得很好,正如我的 App 类中所指定的: prot
我正在用不同的项目编写解决方案。我使用caliburn micro编写了2个wpf客户端。它们都具有一个 map 控件,该控件绘制了一些图层和一个listview。我想在不同项目中托管通用控件,以便两
在尝试学习 GameLibrary 示例应用程序的源代码时,我看到了这样一行: ConventionManager.AddElementConvention(Rating.ValueProperty,
我正在使用 C# 和 Caliburn。 是否有一种全局方式来捕获所有 View 模型中的所有异常? 如果从 DI/IoC 容器导入过程中出现某种异常怎么办? 我基本上想在发生这种情况时显示一个消息框
位于http://caliburnmicro.com的Caliburn.Micro主页提出以下声明,但我无法使用该示例中可以想到的任何变体使CM与PasswordBox控件一起使用。因为名称不同,所以
我正在迈出 MVVM 的第一步。 我正在使用 Caliburn.Micro 作为我的 MVVM 框架。 我有以下代码: using System; using System.Collections.G
我正在开发一个使用 Caliburn Micro 的 WPF 项目。我遇到了一个问题,即第二次打开 View 时, View 中的控件没有得到更新。第一次数据绑定(bind)工作正常。 当我第二次调用
在我的 WPF Caliburn.Micro 应用程序中,我有一个 TabControl。我希望能够根据需要关闭标签。我在这里找到了一种方法: http://devlicio.us/blogs/rob
我是一名优秀的程序员,十分优秀!