MVVM - テキストボックス内のテキストを選択するにはどうすればよいですか?
-
25-09-2019 - |
質問
MVVM でテキストボックス内のテキストを選択する方法はありますか?私が使用している MVVM フレームワークは、Laurent Bugnion の MVVM Light Toolkit です。
解決
「純粋な」MVVM アプリケーション (View にコードビハインドがない) で View に直接影響を与えようとするときは、常に次を使用します。 添付プロパティ 私が達成しようとしている効果をカプセル化するためです。カスタム イベントを使用して実行したいアクションを定義するインターフェイスを作成します。次に、ビュー上でこれらのコマンドを「実行」する各 ViewModel にこのインターフェイスを実装します。最後に、ViewModel を View 定義の添付プロパティにバインドします。次のコードは、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);
}
私の ViewModel 定義:
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
所属していません StackOverflow