MVVM-我怎样才能选择一个文本框的文本? [英] MVVM- How can I select text in a textbox?

查看:139
本文介绍了MVVM-我怎样才能选择一个文本框的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种MVVM的方式来选择一个文本框的文本?该MVVM框架,我现在用的就是洛朗•比尼翁的MVVM光工具包。

Is there a MVVM way to select text in a textbox? The MVVM framework that I am using is Laurent Bugnion's MVVM Light Toolkit.

推荐答案

每当我试图直接影响纯MVVM应用程序的视图(没有code隐藏在View),我将使用< A HREF =htt​​p://msdn.microsoft.com/en-us/library/ms749011.aspx>附加属性封装什么效果,我想实现。我将创建一个定义我想使用自定义的事件采取行动的接口。然后,我实现将跑上查看这些命令每个视图模型此接口。最后,我在我的视图模型绑定到我的视图定义的附加属性。下面code显示了如何以此为全选和一个TextBox。这code可以很容易地扩展到有关在视图中的任何组件上的任何动作只是执行。

Whenever I am trying to directly affect the the View in a "pure" MVVM application (no code-behind in View), I will use Attached Properties to encapsulate whatever effect I am trying to achieve. I will create an interface that defines the actions I wish to take using custom events. I then implement this interface in each ViewModel that will be "running" these commands on the View. Finally, I bind my ViewModel to the attached property in my View definition. The following code shows how to this for SelectAll and a TextBox. This code can be easily expanded to perform just about any action on any component in the View.

我的附加属性和接口定义:

My Attached Property and interface definition:

using System.Windows;
using System.Windows.Controls;
using System;
using System.Collections.Generic;

namespace SelectAllSample
{
    public static class TextBoxAttach
    {
        public static readonly DependencyProperty TextBoxControllerProperty = DependencyProperty.RegisterAttached(
            "TextBoxController", typeof(ITextBoxController), typeof(TextBoxAttach),
            new FrameworkPropertyMetadata(null, OnTextBoxControllerChanged));
        public static void SetTextBoxController(UIElement element, ITextBoxController value)
        {
            element.SetValue(TextBoxControllerProperty, value);
        }
        public static ITextBoxController GetTextBoxController(UIElement element)
        {
            return (ITextBoxController)element.GetValue(TextBoxControllerProperty);
        }

        private static readonly Dictionary<ITextBoxController, TextBox> elements = new Dictionary<ITextBoxController, TextBox>();
        private static void OnTextBoxControllerChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var element = d as TextBox;
            if (element == null)
                throw new ArgumentNullException("d");

            var oldController = e.OldValue as ITextBoxController;
            if (oldController != null)
            {
                elements.Remove(oldController);
                oldController.SelectAll -= SelectAll;
            }

            var newController = e.NewValue as ITextBoxController;
            if (newController != null)
            {
                elements.Add(newController, element);
                newController.SelectAll += SelectAll;
            }
        }
        private static void SelectAll(ITextBoxController sender)
        {
            TextBox element;
            if (!elements.TryGetValue(sender, out element))
                throw new ArgumentException("sender");
            element.Focus();
            element.SelectAll();
        }
    }

    public interface ITextBoxController
    {
        event SelectAllEventHandler SelectAll;
    }

    public delegate void SelectAllEventHandler(ITextBoxController sender);
}

我的视图模型的定义:

My ViewModel definition:

public class MyViewModel : ITextBoxController
{
    public MyViewModel()
    {
        Value = "My Text";
        SelectAllCommand = new RelayCommand(p =>
        {
            if (SelectAll != null)
                SelectAll(this);
        });
    }

    public string Value { get; set; }
    public RelayCommand SelectAllCommand { get; private set; }

    public event SelectAllEventHandler SelectAll;
}

我的视图定义:

<Window x:Class="SelectAllSample.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:loc="clr-namespace:SelectAllSample"
    Title="Window1" Height="150" Width="150">
    <x:Code><![CDATA[
        public Window1()
        {
            InitializeComponent();
            DataContext = new MyViewModel();
        }
    ]]></x:Code>
    <StackPanel>
        <TextBox Text="{Binding Value}" loc:TextBoxAttach.TextBoxController="{Binding}" />
        <Button Content="Select All" Command="{Binding SelectAllCommand}" />
    </StackPanel>
</Window>

注意:感谢约什 - 史密斯为RelayCommand(见图3 code上的此页面)。它在MyViewModel本例中使用(和几乎所有我的MVVM code)。

Note: Thanks to Josh Smith for RelayCommand (see code in Figure 3 on this page). It is used in MyViewModel in this example (and just about all my MVVM code).

这篇关于MVVM-我怎样才能选择一个文本框的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆