在一个列表控件中,每个数据模板中 比如有按钮,单击事件绑定vm的命令,一般都是找到最根部的datacontext,那么在cm中有更简单的方式
如何将动作从子视图模型冒泡到父视图
准备工作
添加 BubblingViewModel
using Caliburn.Micro; namespace cm1.ViewModels { public class BubblingViewModel : Screen { public BindableCollection<MessageActivityViewModel> Phrases { get; } = new BindableCollection<MessageActivityViewModel> { new MessageActivityViewModel("我是第1条"), new MessageActivityViewModel("我是第2条"), new MessageActivityViewModel("我是第3条"), new MessageActivityViewModel("我是第4条") }; //public IEnumerable<IResult> SelectPhrase(MessageActivityViewModel phrase) //{ // yield return new MessageDialogResult($"选中的内容是 {phrase.Message}.", "选中"); //} public void SelectPhrase(MessageActivityViewModel phrase) { //Console($"选中的内容是 {phrase.Message}.", "选中"); System.Console.WriteLine($"选中的内容是 { phrase.Message}"); } } }
打开Bootstrapper.cs 修改
protected override void Configure() { container = new SimpleContainer(); container.Instance(container); container .Singleton<IWindowManager, WindowManager>() .Singleton<IEventAggregator, EventAggregator>(); container .PerRequest<ShellViewModel>() .PerRequest<MenuViewModel>() .PerRequest<BindingsViewModel>() .PerRequest<ActionsViewModel>() .PerRequest<CoroutineViewModel>() .PerRequest<ExecuteViewModel>() .PerRequest<EventAggregationViewModel>() .PerRequest<ConductorViewModel>() .PerRequest<BubblingViewModel>(); }
打开MenuViewModel.cs添加菜单
Features = new BindableCollection<FeatureViewModel> { new FeatureViewModel("Binding Conventions", "Binding view model properties to your view.", typeof(BindingsViewModel)), new FeatureViewModel("Action Conventions", "Wiring view events to view model methods.", typeof(ActionsViewModel)), new FeatureViewModel("Coroutines", "Using IEnumerable<IResult>", typeof(CoroutineViewModel)), new FeatureViewModel("Execute", "Using Execute to execute code on the UI thread.", typeof(ExecuteViewModel)), new FeatureViewModel("Event Aggregator", "在非耦合视图模型之间发送事件.", typeof(EventAggregationViewModel)),\ new FeatureViewModel("Bubbling Actions", "如何将动作从子视图模型冒泡到父视图", typeof(BubblingViewModel)), };
添加 BubblingView
<Page x:Class="cm1.Views.BubblingView" 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:cal="clr-namespace:Caliburn.Micro;assembly=Caliburn.Micro.Platform" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300" Title="BubblingView"> <Grid> <ScrollViewer Padding="24,12"> <ItemsControl x:Name="Phrases"> <ItemsControl.ItemTemplate> <DataTemplate> <Grid cal:Bind.Model="{Binding}"> <Button x:Name="Message" cal:Message.Attach="SelectPhrase($dataContext)" Margin="0,12" /> </Grid> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> </ScrollViewer> </Grid> </Page>
在 数据模板的根上使用 cal:Bind.Model="{Binding}"
Attach会冒泡到符合的方法
运行
修改方法void
效果一样
本文由 安徽 合肥 杨洋 1991年的 英文名AY ===来自网站 www.ayjs.net 编写
本文由 安徽 合肥 杨洋 1991年的 英文名AY ===来自网站 www.ayjs.net 编写
本文代码下载: