Quantcast
Channel: かずきのBlog@hatena
Viewing all articles
Browse latest Browse all 1387

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

$
0
0

blog.okazuki.jp

続きです。

senderのDataContextを引数に渡してくれるようなCallMethodActionを自作すればいいですね。

using Microsoft.Xaml.Interactivity;
using System.Reflection;
using Windows.UI.Xaml;

namespace App9
{
    publicclass TransferSenderDataContextCallMethodAction : DependencyObject, IAction
    {
        publicobject 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...publicstaticreadonly DependencyProperty TargetObjectProperty =
            DependencyProperty.Register("TargetObject", typeof(object), typeof(TransferSenderDataContextCallMethodAction), new PropertyMetadata(null));

        publicstring 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...publicstaticreadonly DependencyProperty MethodNameProperty =
            DependencyProperty.Register("MethodName", typeof(string), typeof(TransferSenderDataContextCallMethodAction), new PropertyMetadata(null));



        publicobject Execute(object sender, object parameter)
        {
            if (this.TargetObject == null || this.MethodName == null)
            {
                returnnull;
            }

            var methodInfo = this.TargetObject.GetType().GetTypeInfo().GetDeclaredMethod(this.MethodName);
            return methodInfo.Invoke(this.TargetObject, newobject[] { (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>publicsealedpartialclass MainPage : Page
    {
        public MainPageViewModel ViewModel => this.DataContext as MainPageViewModel;

        public MainPage()
        {
            this.InitializeComponent();
        }
    }

    publicclass MainPageViewModel : BindableBase
    {
        public ObservableCollection<ItemViewModel> Items { get; } = new ObservableCollection<ItemViewModel>
        {
            new ItemViewModel { Value = "Item1" },
            new ItemViewModel { Value = "Item2" },
            new ItemViewModel { Value = "Item3" },
        };

        publicvoid Alert(ItemViewModel item)
        {
            Debug.WriteLine($"Alert {item.Value}");
        }
    }

    publicclass ItemViewModel : BindableBase
    {
        privatestringvalue;

        publicstring Value
        {
            get { returnthis.value; }
            set { this.SetProperty(refthis.value, value); }
        }

    }
}

Viewing all articles
Browse latest Browse all 1387

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>