かずきのBlog@hatena

すきな言語は C# + XAML の組み合わせ。Azure Functions も好き。最近は Go 言語勉強中。日本マイクロソフトで働いていますが、ここに書いていることは個人的なメモなので会社の公式見解ではありません。

UWPのListViewのItemTemplate内のボタンをクリックしたときにPageのDataContextにセットしたViewModelのメソッドを呼んでかつ押された行のデータが知りたい

blog.okazuki.jp

続きです。

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); }
        }

    }
}