基于MVVM实现WPF作业中Copy、Clone按钮功能的技术求助
WPF MVVM模式下Copy/Clone按钮功能实现示例
核心逻辑说明
两个功能的差异本质是绑定源的独立性不同:
- Copy按钮:每次生成的新文本框绑定独立的字符串属性,各文本框值互不影响
- Clone按钮:所有Clone生成的文本框绑定同一个全局共享属性,属性值变更时会自动通知所有绑定控件同步更新,无需手动遍历修改
完整实现代码
1. 基础类与ViewModel实现
首先实现MVVM所需的属性变更通知基类,以及动态文本项的包装类:
using System.ComponentModel; using System.Runtime.CompilerServices; using System.Collections.ObjectModel; using System.Windows.Input; using CommunityToolkit.Mvvm.Input; // 属性变更通知基类 public class ObservableObject : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged([CallerMemberName] string propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } // 动态文本项包装类 public class TextItem : ObservableObject { // 普通Copy项的独立存储字段 private string _text; // 标识是否为Clone项 public bool IsClone { get; set; } // 关联的全局共享Clone文本引用(由外层ViewModel传入) public Func<string> GetSharedText; public Action<string> SetSharedText; public string Text { get => IsClone ? GetSharedText() : _text; set { if (IsClone) { SetSharedText(value); } else { _text = value; OnPropertyChanged(); } } } } // 主窗口ViewModel public class MainViewModel : ObservableObject { // 原始输入文本框绑定属性 private string _originalText; public string OriginalText { get => _originalText; set { _originalText = value; OnPropertyChanged(); } } // 所有Clone项共享的文本属性 private string _sharedCloneText; public string SharedCloneText { get => _sharedCloneText; set { _sharedCloneText = value; OnPropertyChanged(); // 通知所有Clone项更新值 foreach (var item in DynamicTextItems) { if (item.IsClone) item.OnPropertyChanged(nameof(TextItem.Text)); } } } // 动态生成的文本项集合,界面ItemsControl绑定此集合 public ObservableCollection<TextItem> DynamicTextItems { get; set; } = new(); // Copy按钮命令 public ICommand CopyCommand { get; } // Clone按钮命令 public ICommand CloneCommand { get; } public MainViewModel() { CopyCommand = new RelayCommand(ExecuteCopy); CloneCommand = new RelayCommand(ExecuteClone); } private void ExecuteCopy() { DynamicTextItems.Add(new TextItem { IsClone = false, Text = OriginalText }); } private void ExecuteClone() { // 首次点击Clone时先初始化共享文本为当前原始文本 if (string.IsNullOrEmpty(SharedCloneText)) { SharedCloneText = OriginalText; } DynamicTextItems.Add(new TextItem { IsClone = true, GetSharedText = () => SharedCloneText, SetSharedText = v => SharedCloneText = v }); } }
2. 主界面XAML实现
<Window x:Class="WpfApp.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfApp" Title="MainWindow" Height="450" Width="800"> <Window.DataContext> <local:MainViewModel /> </Window.DataContext> <StackPanel Margin="20"> <!-- 原始输入文本框 --> <TextBox Text="{Binding OriginalText}" Height="30" PlaceholderText="输入原始内容" Margin="0 0 0 10"/> <!-- 功能按钮区 --> <StackPanel Orientation="Horizontal" Margin="0 0 0 10"> <Button Content="Copy" Command="{Binding CopyCommand}" Width="100" Height="30" Margin="0 0 10 0"/> <Button Content="Clone" Command="{Binding CloneCommand}" Width="100" Height="30"/> </StackPanel> <!-- 动态生成的文本框区域 --> <ItemsControl ItemsSource="{Binding DynamicTextItems}"> <ItemsControl.ItemTemplate> <DataTemplate> <TextBox Text="{Binding Text, UpdateSourceTrigger=PropertyChanged}" Height="30" Margin="0 5 0 0"/> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> </StackPanel> </Window>
Clone功能核心说明
所有Clone生成的TextItem不会自己存储文本值,读写都直接操作ViewModel层的全局SharedCloneText属性。当SharedCloneText发生变更时,会主动遍历所有Clone项触发属性变更通知,所有绑定了这些项的文本框就会自动同步最新值,满足联动需求。
内容的提问来源于stack exchange,提问作者Junior KS
相关产品推荐
相关产品推荐

