使用MVVM在WPF文本框中选择和复制文本

时间:2013-08-05 14:22:14

标签: c# wpf mvvm textbox clipboard

我发现这个问题可以帮助我实现我想做的部分:MVVM- How can I select text in a textbox?

我的第一个问题是我有两个文本框,每个都有一个“全选”按钮,但我无法弄清楚如何调整接受的答案,以便我可以独立控制每个。

另外,我想为每个添加“复制所选文本”按钮。

如何在坚持MVVM模式的同时做到这一点?

1 个答案:

答案 0 :(得分:4)

您可以将两个按钮绑定到调用不同文本框上的不同命令,也可以使用commandParameters来区分要处理的文本框。

您可以通过创建AttachedProperty或仅制作自定义控件来完成链接的帖子。您需要做的就是为文本选择创建一个可绑定属性。 TextBox的属性“SelectedText”听起来是个好主意,但如果你试图在WPF中绑定它,它会抛出一个错误,因为它不是DependencyProperty。属性必须是DependencyProperty才能绑定到它。

所以你创建一个,例如IsTextSelected作为bool,当它改变时,你的AttachedProperty或自定义控件处理它并执行SelectAll()或者SelectedText = Text;

如果做单个项目,我建议使用AttachedProperty。但是,您要求自定义控件,我相信如果对一种类型的控件进行多项功能改进,应该使用它们,而不能在不同类型上重复使用它们。

public class SmartTextBox : TextBox
{
    public static readonly DependencyProperty IsSelectedTextProperty = DependencyProperty.RegisterAttached("IsSelectedText",
        typeof(bool), typeof(SmartTextBox), new FrameworkPropertyMetadata(false, OnIsSelectedChanged));

    public bool IsSelectedText
    {
        get { return (bool)GetValue(IsSelectedTextProperty); }
        set { SetValue(IsSelectedTextProperty, value); }
    }

    private static void OnIsSelectedChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
    {
        SmartTextBox textbox = sender as SmartTextBox;
        if ((bool)e.NewValue)
        {
            textbox.Focus();
            textbox.SelectAll();
        }
    }
}

用法:ViewModel

  • 注意1我没有在set期间对值是否相同进行IF,以便您可以随时强制执行,而不是跟踪用户的操作。
  • 注意2创建多个属性,IsSelectedUsername,IsSelectedFilepath等,然后绑定它们。每个SmartTextBox都绑定到一个,并将处理一个更改。

    public bool IsSelectedText
    {
        get { return isSelectedText; }
        set
        {
            isSelectedText = value;
            RaisePropertyChanged("IsSelectedText");
        }
    }
    
    private void SelectAllExecute()
    {
        IsSelectedText = true;
    }
    

用法:XAML

xmlns:custom="clr-namespace:xyz.View.Controls"

        <custom:SmartTextBox Text="{Binding Path=MyText}" 
                             IsSelectedText="{Binding Path=IsSelectedText}"/>

检索所选文本,您需要向自定义控件添加可绑定的新依赖项属性以及控件更新它的方法。我选择控件离开焦点而不是选择更改,因为我希望用户在我需要知道所选文本之前点击一个按钮。

    public static readonly DependencyProperty SelectedText2Property = DependencyProperty.RegisterAttached("SelectedText2",
        typeof(string), typeof(SmartTextBox), new PropertyMetadata(""));

    public string SelectedText2
    {
        get { return (string)GetValue(SelectedText2Property); }
        set { SetValue(SelectedText2Property, value); }
    }

    protected override void OnLostFocus(RoutedEventArgs e)
    {
        SelectedText2 = this.SelectedText;
        base.OnLostFocus(e);
    }

XAML现在受到约束:

        <custom:SmartTextBox Text="{Binding Path=MyText}" 
                             SelectedText2="{Binding Path=TheSelectedText, Mode=OneWayToSource}"
                             IsSelectedText="{Binding Path=IsSelectedText}"/>

ViewModel有一个哑属性(因为它是OneWayToSource,所以不需要引发更改事件)

public string TheSelectedText { get; set; }

你可以做的任何地方

Console.WriteLine(TheSelectedText);