有没有 MVVM 方式来选择文本框中的文本?我使用的 MVVM 框架是 Laurent Bugnion 的 MVVM Light Toolkit。

有帮助吗?

解决方案

每当我尝试直接影响“纯”MVVM 应用程序中的视图(视图中没有代码隐藏)时,我都会使用 附加属性 封装我想要达到的任何效果。我将创建一个界面,定义我希望使用自定义事件采取的操作。然后,我在每个将在视图上“运行”这些命令的 ViewModel 中实现此接口。最后,我将 ViewModel 绑定到视图定义中的附加属性。以下代码演示了如何对 SelectAll 和 TextBox 执行此操作。可以轻松扩展此代码以对视图中的任何组件执行几乎任何操作。

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

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

我的视图模型定义:

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>

笔记: 感谢 Josh Smith 提供的 RelayCommand(参见图 3 中的代码) 这一页)。在此示例中,它在 MyViewModel 中使用(以及几乎所有我的 MVVM 代码)。

其他提示

找到一个很好的介绍此处附加属性: http://www.codeproject.com/KB/WPF/AttachedBehaviors.aspx

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top