c# - WPF MVVM如何在ViewModel中处理TextBox“粘贴事件”

69 篇文章 3 订阅
public class TextBoxPasteBehavior 
{
public static readonly DependencyProperty PasteCommandProperty =
    DependencyProperty.RegisterAttached(
        "PasteCommand",
        typeof(ICommand),
        typeof(TextBoxPasteBehavior),
        new FrameworkPropertyMetadata(PasteCommandChanged)
    );

public static ICommand GetPasteCommand(DependencyObject target)
{
    return (ICommand)target.GetValue(PasteCommandProperty);
}

public static void SetPasteCommand(DependencyObject target, ICommand value)
{
    target.SetValue(PasteCommandProperty, value);
}

static void PasteCommandChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
    var textBox = (TextBox)sender;
    var newValue = (ICommand)e.NewValue;

    if (newValue != null)
        textBox.AddHandler(CommandManager.ExecutedEvent, new RoutedEventHandler(CommandExecuted), true);
    else
        textBox.RemoveHandler(CommandManager.ExecutedEvent, new RoutedEventHandler(CommandExecuted));

}

static void CommandExecuted(object sender, RoutedEventArgs e)
{
    if (((ExecutedRoutedEventArgs)e).Command != ApplicationCommands.Paste) return;

    var textBox = (TextBox)sender;
    var command = GetPasteCommand(textBox);

    if (command.CanExecute(null))
        command.Execute(textBox);
}
}

在XAML中使用。在TextBox中作为属性。
 

TextBoxPasteBehavior.PasteCommand="{Binding PropertyGridTextPasted}"



PropertyGridTextPasted-ViewModel中的命令。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPF (Windows Presentation Foundation)是一种用于创建 Windows 桌面应用程序的技术。MVVM (Model-View-ViewModel) 是一种设计模式,用于分离应用程序的逻辑、数据和界面。 要在WPF应用程序使用MVVM模式实现TextBox的功能,你可以按照以下步骤操作: 1. 创建一个Model类,该类包含与TextBox相关的数据。例如,你可以创建一个名为"User"的类,其包含一个名为"Name"的属性来表示用户输入的文本。 2. 创建一个ViewModel类,该类充当Model和View之间的间层。ViewModel类应该包含一个可绑定的属性,用于将TextBox的文本与Model的数据进行绑定。在ViewModel,你可以使用实现了INotifyPropertyChanged接口的属性,以便在文本更改时通知View更新。 3. 创建一个View类,该类表示用户界面。在View,通过使用XAML语法,将TextBoxViewModel的属性进行绑定。这样,当用户在TextBox输入文本时,ViewModel的属性将自动更新。 下面是一个简单的示例: Model类: ```C# public class User { public string Name { get; set; } } ``` ViewModel类: ```C# public class UserViewModel : INotifyPropertyChanged { private User user; public UserViewModel() { user = new User(); } public string UserName { get { return user.Name; } set { if (user.Name != value) { user.Name = value; OnPropertyChanged(nameof(UserName)); } } } public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } ``` View的XAML代码: ```XAML <Window x:Class="YourNamespace.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:YourNamespace" Title="Your Application" Height="450" Width="800"> <Grid> <TextBox Text="{Binding UserName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" /> </Grid> </Window> ``` 请注意,上述示例只是一个简化的实现,你可以根据自己的需求进行扩展和修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值