恭喜,你发布的帖子
发布于 2020-02-01 22:25:08
7楼
3.3 通过自定义对象,处理View中的事件。这里用了另一个界面来测试,只有一个按钮来触发事件。
界面的Xaml代码如下:
<Window x:Class="WpfApp02.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:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:local="clr-namespace:WpfApp02"
mc:Ignorable="d"
Title="MainWindow" Height="145" Width="220">
<Window.DataContext>
<local:MainWindowViewModel/>
</Window.DataContext>
<Grid>
<Button Content="Button" Name="button1" VerticalAlignment="Center" Width="100">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<local:ExInvokeCommandAction
Command="{Binding ClickCommand}"
CommandParameter="{Binding ElementName=button1}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
</Grid>
</Window>
与MainWindow对应的MainWindowViewModel的C#代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using Prism.Commands;
using System.Windows;
namespace WpfApp02
{
public class MainWindowViewModel
{
public ICommand ClickCommand
{
get
{
return new DelegateCommand<ExCommandParameter>((p) =>
{
RoutedEventArgs args = p.EventArgs as RoutedEventArgs;
MessageBox.Show(args.ToString());
},
(p) =>
{
return true;
}
);
}
}
}
}
这里只是简单展示ViewModel得到的来自UI的消息性质。实用中把消息拆包,会根据内容做出不同的响应。
请填写推广理由:
分享
只看
楼主