背景说明
项目日常应用,经常会使用到UserConrol来进行组合形成组合控件,组合控件在使用过程中,必然需要进行赋值,当前案例是UserControl中label的定时赋值。
技术分析
1.分离关注点:MVVM 模式将应用程序分为三个主要部分,即模型(Model)、视图(View)和视图模型(ViewModel)。在这个例子中,MyUserControlViewModel
和 MainViewModel
充当 ViewModel,负责处理业务逻辑和数据操作;MyUserControl.xaml
和 MainWindow.xaml
是 View,负责呈现用户界面;而数据本身则相当于 Model。这种分离使得代码结构更加清晰,易于维护和测试。
2.属性更改通知:ViewModel 实现了 INotifyPropertyChanged
接口,当 ViewModel 中的属性值发生变化时,会触发 PropertyChanged
事件,通知视图更新相应的显示内容。这是 MVVM 模式中实现数据绑定自动更新的关键技术。
代码
UserControl的xaml
<UserControl x:Class="UserControlLabelsShow.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:UserControlLabelsShow"
mc:Ignorable="d" Background="White" Height="145" Width="115"
>
<Grid >
<Label Content="{Binding Label1Value}" Foreground="Black" HorizontalAlignment="Left" VerticalAlignment="Top" Height="25" Width="140"/>
<Label Content="{Binding Label2Value}" Foreground="Black" HorizontalAlignment="Left" Margin="0,30,0,0" VerticalAlignment="Top" Height="30" Width="140"/>
<Label Content="{Binding Label3Value}" Foreground="Black" HorizontalAlignment="Left" Margin="0,60,0,0" VerticalAlignment="Top" Height="25" Width="140"/>
<Label Content="{Binding Label4Value}" Foreground="Black" HorizontalAlignment="Left" VerticalAlignment="Top" Height="30" Width="140" Margin="0,90,0,0"/>
<Label Content="{Binding Label5Value}" Foreground="Black" HorizontalAlignment="Left" Margin="0,115,0,0" VerticalAlignment="Top" Height="25" Width="140"/>
</Grid>
</UserControl>
UserControl的cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace UserControlLabelsShow
{
/// <summary>
/// UserControl1.xaml 的交互逻辑
/// </summary>
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
DataContext = new UserControl1ViewModel();
}
}
}
UserControl1ViewModel
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace UserControlLabelsShow
{
public class UserControl1ViewModel:Notify
{
private int _label1Value;
private int _label2Value;
private int _label3Value;
private int _label4Value;
private int _label5Value;
public int Label1Value
{
get { return _label1Value; }
set
{
if (_label1Value != value)
{
_label1Value = value;
OnPropertyChanged(nameof(Label1Value));
}
}
}
public int Label2Value
{
get { return _label2Value; }
set
{
if (_label2Value != value)
{
_label2Value = value;
OnPropertyChanged(nameof(Label2Value));
}
}
}
public int Label3Value
{
get { return _label3Value; }
set
{
if (_label3Value != value)
{
_label3Value = value;
OnPropertyChanged(nameof(Label3Value));
}
}
}
public int Label4Value
{
get { return _label4Value; }
set
{
if (_label4Value != value)
{
_label4Value = value;
OnPropertyChanged(nameof(Label4Value));
}
}
}
public int Label5Value
{
get { return _label5Value; }
set
{
if (_label5Value != value)
{
_label5Value = value;
OnPropertyChanged(nameof(Label5Value));
}
}
}
}
}
MainWindowViewModel.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
namespace UserControlLabelsShow
{
public class MainWindowViewModel: Notify
{
public UserControl1ViewModel UserControl1ViewModel { get; set; }
public UserControl1ViewModel UserControl2ViewModel { get; set; }
public UserControl1ViewModel UserControl3ViewModel { get; set; }
public UserControl1ViewModel UserControl4ViewModel { get; set; }
private Timer _timer;
public MainWindowViewModel()
{
UserControl1ViewModel = new UserControl1ViewModel();
UserControl2ViewModel = new UserControl1ViewModel();
UserControl3ViewModel = new UserControl1ViewModel();
UserControl4ViewModel = new UserControl1ViewModel();
_timer = new Timer(TimerCallback, null, TimeSpan.Zero, TimeSpan.FromSeconds(1));
}
private void TimerCallback(object state)
{
Application.Current.Dispatcher.Invoke(() =>
{
// 第一个 UserControl 赋值
UserControl1ViewModel.Label1Value++;
UserControl1ViewModel.Label2Value++;
UserControl1ViewModel.Label3Value++;
UserControl1ViewModel.Label4Value++;
UserControl1ViewModel.Label5Value++;
// 第二个 UserControl 赋值
UserControl2ViewModel.Label1Value = UserControl2ViewModel.Label1Value + 100;
UserControl2ViewModel.Label2Value = UserControl2ViewModel.Label2Value + 100;
UserControl2ViewModel.Label3Value = UserControl2ViewModel.Label3Value + 100;
UserControl2ViewModel.Label4Value = UserControl2ViewModel.Label4Value + 100;
UserControl2ViewModel.Label5Value = UserControl2ViewModel.Label5Value + 100;
// 第三个 UserControl 赋值
UserControl3ViewModel.Label1Value = UserControl3ViewModel.Label1Value + 200;
UserControl3ViewModel.Label2Value = UserControl3ViewModel.Label2Value + 200;
UserControl3ViewModel.Label3Value = UserControl3ViewModel.Label3Value + 200;
UserControl3ViewModel.Label4Value = UserControl3ViewModel.Label4Value + 200;
UserControl3ViewModel.Label5Value = UserControl3ViewModel.Label5Value + 200;
// 第四个 UserControl 赋值
UserControl4ViewModel.Label1Value = UserControl4ViewModel.Label1Value + 300;
UserControl4ViewModel.Label2Value = UserControl4ViewModel.Label2Value + 300;
UserControl4ViewModel.Label3Value = UserControl4ViewModel.Label3Value + 300;
UserControl4ViewModel.Label4Value = UserControl4ViewModel.Label4Value + 300;
UserControl4ViewModel.Label5Value = UserControl4ViewModel.Label5Value + 300;
});
}
}
}
MainWindow.xaml
<Window x:Class="UserControlLabelsShow.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:UserControlLabelsShow"
mc:Ignorable="d"
Title="MainWindow" Height="500" Width="690">
<Grid>
<local:UserControl1 DataContext="{Binding UserControl1ViewModel}" HorizontalAlignment="Left" Margin="10,5,0,0" VerticalAlignment="Top" Height="140" Width="305" Background="Aqua"/>
<local:UserControl1 DataContext="{Binding UserControl2ViewModel}" HorizontalAlignment="Left" Margin="15,170,0,0" VerticalAlignment="Top" Height="140" Width="305" Background="Aqua"/>
<local:UserControl1 DataContext="{Binding UserControl3ViewModel}" HorizontalAlignment="Left" Margin="350,170,0,0" VerticalAlignment="Top" Height="140" Width="305" Background="Aqua"/>
<local:UserControl1 DataContext="{Binding UserControl4ViewModel}" HorizontalAlignment="Left" Margin="350,5,0,0" VerticalAlignment="Top" Height="140" Width="305" Background="Aqua"/>
</Grid>
</Window>
MainWindow.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace UserControlLabelsShow
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new MainWindowViewModel();
}
}
}
辅助类MyCommand
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace UserControlLabelsShow
{
public class MyCommand:ICommand
{
private readonly Action _execute;
private readonly Func<bool> _canExecute;
public MyCommand(Action execute, Func<bool> canExecute = null)
{
_execute = execute ?? throw new ArgumentNullException(nameof(execute));
_canExecute = canExecute;
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public bool CanExecute(object parameter)
{
return _canExecute == null || _canExecute();
}
public void Execute(object parameter)
{
_execute();
}
}
}
辅助类Notify
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
namespace UserControlLabelsShow
{
public abstract class Notify : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged([CallerMemberName] string name = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}
}