加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何使用Caliburn.Micro MVVM将焦点设置到控件

发布时间:2020-12-15 08:33:02 所属栏目:百科 来源:网络整理
导读:我有一个表单,我想在某些用户操作发生时将焦点设置为文本框.我知道MVVM的做法是绑定到VM属性,但是TextBox没有允许这种情况发生的属性.从VM设置焦点的最佳方法是什么? 解决方法 我已经创建了一个IResult实现,它可以很好地实现这一目标.您可以从IResult的Acti
我有一个表单,我想在某些用户操作发生时将焦点设置为文本框.我知道MVVM的做法是绑定到VM属性,但是TextBox没有允许这种情况发生的属性.从VM设置焦点的最佳方法是什么?

解决方法

我已经创建了一个IResult实现,它可以很好地实现这一目标.您可以从IResult的ActionExecutionContext获取视图,然后您可以搜索(我按名称搜索)您要关注的控件.
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();
    }
}

我已经省略了一些额外的代码,以便在视图是我可以提供的ChildWindow时从上下文中获取视图.

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,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,results);
        }
    }
}

您的操作将类似于以下内容(在Caliburn.Micro ActionMessage样式中调用).

public IEnumerable<IResult> MyAction()
{
    // do whatever
    yield return new GiveFocusByName("NameOfControlToFocus");
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读