ç¶ãã§ãã
senderã®DataContextã弿°ã«æ¸¡ãã¦ããããããªCallMethodActionãèªä½ããã°ããã§ããã
using Microsoft.Xaml.Interactivity; using System.Reflection; using Windows.UI.Xaml; namespace App9 { public class TransferSenderDataContextCallMethodAction : DependencyObject, IAction { public object TargetObject { get { return (object)GetValue(TargetObjectProperty); } set { SetValue(TargetObjectProperty, value); } } // Using a DependencyProperty as the backing store for TargetObject. This enables animation, styling, binding, etc... public static readonly DependencyProperty TargetObjectProperty = DependencyProperty.Register("TargetObject", typeof(object), typeof(TransferSenderDataContextCallMethodAction), new PropertyMetadata(null)); public string MethodName { get { return (string)GetValue(MethodNameProperty); } set { SetValue(MethodNameProperty, value); } } // Using a DependencyProperty as the backing store for MethodName. This enables animation, styling, binding, etc... public static readonly DependencyProperty MethodNameProperty = DependencyProperty.Register("MethodName", typeof(string), typeof(TransferSenderDataContextCallMethodAction), new PropertyMetadata(null)); public object Execute(object sender, object parameter) { if (this.TargetObject == null || this.MethodName == null) { return null; } var methodInfo = this.TargetObject.GetType().GetTypeInfo().GetDeclaredMethod(this.MethodName); return methodInfo.Invoke(this.TargetObject, new object[] { (sender as FrameworkElement)?.DataContext }); } } }
ããã¤ã¯ãããªæãã§ä½¿ãã
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:App9" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:Interactivity="using:Microsoft.Xaml.Interactivity" xmlns:Core="using:Microsoft.Xaml.Interactions.Core" x:Class="App9.MainPage" mc:Ignorable="d" x:Name="Root"> <Page.DataContext> <local:MainPageViewModel /> </Page.DataContext> <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <ListView ItemsSource="{x:Bind ViewModel.Items}"> <ListView.ItemTemplate> <DataTemplate x:DataType="local:ItemViewModel"> <StackPanel> <TextBlock Text="{x:Bind Value}" /> <Button Content="OKOK"> <Interactivity:Interaction.Behaviors> <Core:EventTriggerBehavior EventName="Click"> <local:TransferSenderDataContextCallMethodAction TargetObject="{Binding ElementName=Root, Path=DataContext}" MethodName="Alert" /> </Core:EventTriggerBehavior> </Interactivity:Interaction.Behaviors> </Button> </StackPanel> </DataTemplate> </ListView.ItemTemplate> </ListView> </Grid> </Page>
VMã®ã¡ã½ããã¯å¼æ°ãå¢ããã¦ããã¾ãããã
using Prism.Mvvm; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Diagnostics; using System.IO; using System.Linq; using System.Runtime.InteropServices.WindowsRuntime; using Windows.Foundation; using Windows.Foundation.Collections; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls.Primitives; using Windows.UI.Xaml.Data; using Windows.UI.Xaml.Input; using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Navigation; // 空ç½ãã¼ã¸ã®ã¢ã¤ãã ãã³ãã¬ã¼ãã«ã¤ãã¦ã¯ãhttp://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409 ãåç §ãã¦ãã ãã namespace App9 { /// <summary> /// ããèªä½ã§ä½¿ç¨ã§ãã空ç½ãã¼ã¸ã¾ãã¯ãã¬ã¼ã å ã«ç§»åã§ãã空ç½ãã¼ã¸ã /// </summary> public sealed partial class MainPage : Page { public MainPageViewModel ViewModel => this.DataContext as MainPageViewModel; public MainPage() { this.InitializeComponent(); } } public class MainPageViewModel : BindableBase { public ObservableCollection<ItemViewModel> Items { get; } = new ObservableCollection<ItemViewModel> { new ItemViewModel { Value = "Item1" }, new ItemViewModel { Value = "Item2" }, new ItemViewModel { Value = "Item3" }, }; public void Alert(ItemViewModel item) { Debug.WriteLine($"Alert {item.Value}"); } } public class ItemViewModel : BindableBase { private string value; public string Value { get { return this.value; } set { this.SetProperty(ref this.value, value); } } } }