近期公司重构了些界面,因为换肤和界面定制的缘故,需要把样式和逻辑分开;所以记录下关键的操作;主要是利用命令代替事件,利用命令传递事件的参数...
先大致看下效果:
主要是利用 Prism 库,可直接利用 nuget 添加
下面是详细代码:
<Window x:Class="Demo_MVVM.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:Demo_MVVM"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:prism="http://www.codeplex.com/prism"
mc:Ignorable="d"
Title="MainWindow"
WindowStartupLocation="CenterScreen"
Height="350"
Width="400"
DataContext="{DynamicResource vm}"
>
<Window.Resources>
<local:ReverseBool x:Key="ReverseBool" />
<DataTemplate x:Key="Template1">
<TextBlock Text="{Binding IsTemplate1,StringFormat=我是模板1:{0}}"/>
</DataTemplate>
<DataTemplate x:Key="Template2">
<TextBlock Text="{Binding IsTemplate1,StringFormat=我是模板2:{0}}"/>
</DataTemplate>
<local:MainWindowViewModel x:Key="vm"/>
</Window.Resources>
<Grid>
<StackPanel>
<TextBlock Text="采用mvvm,UI和逻辑分离" />
<StackPanel Orientation="Horizontal">
<RadioButton Content="模板1" IsChecked="{Binding IsTemplate1}" GroupName="mb" />
<RadioButton Content="模板2" IsChecked="{Binding IsTemplate1,Converter={StaticResource ReverseBool}}" GroupName="mb" />
</StackPanel>
<ContentControl Content="{Binding}">
<ContentControl.Style>
<Style TargetType="ContentControl">
<Setter Property="ContentTemplate" Value="{StaticResource Template1}" />
<Style.Triggers>
<DataTrigger Binding="{Binding IsTemplate1}" Value="false">
<Setter Property="ContentTemplate" Value="{StaticResource Template2}" />
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
<TextBox Text="这里面实现了鼠标滚动获取滚动向量" Height="100" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="PreviewMouseWheel">
<prism:InvokeCommandAction Command="{Binding MouseWheelCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>
</StackPanel>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<prism:InvokeCommandAction Command="{Binding InitCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Grid>
</Window>
using Prism.Commands;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
namespace Demo_MVVM
{
class MainWindowViewModel : INotifyPropertyChanged
{
private bool isTemplate1 = true;
public event PropertyChangedEventHandler PropertyChanged;
public ICommand InitCommand { get; private set; }
public DelegateCommand<object> MouseWheelCommand { get; set; }
public bool IsTemplate1
{
get => isTemplate1;
set
{
isTemplate1 = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsTemplate1)));
}
}
public MainWindowViewModel()
{
InitCommand = new DelegateCommand(Init);
MouseWheelCommand = new DelegateCommand<object>(MouseWheel);
}
void Init()
{
MessageBox.Show("初始化完成!");
}
void MouseWheel(object obj)
{
if (obj is MouseWheelEventArgs eventArgs)
{
MessageBox.Show("滚动了:" + eventArgs.Delta);
}
}
}
}
有需要完整demo的,包含所需要的库,请移步下载