gpt4 book ai didi

c# - MVVM-如何在文本框中选择文本?

转载 作者:可可西里 更新时间:2023-11-01 07:54:39 25 4
gpt4 key购买 nike

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

最佳答案

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

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

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

我的 View 定义:

<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 中的代码,位于 this page 上)。在此示例中,它用在 MyViewModel 中(以及几乎所有我的 MVVM 代码)。

关于c# - MVVM-如何在文本框中选择文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2596757/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com