前提・実現したいこと
ListViewのGroupHeaderTemplateに追加したButtonをICommandインターフェースを使ってクリックイベントを取得したい。
発生している問題・エラーメッセージ
Buttonを押しても反応しない、
該当のソースコード&画面
以下、抜粋されたソースになります。
C#
1<?xml version="1.0" encoding="UTF-8"?> 2<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 3 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 4 x:Class="Test.Views.MainPage" 5 xmlns:viewModel="clr-namespace:Test.ViewModels"> 6 7 <ContentPage.BindingContext> 8 <viewModel:TestViewModel/> 9 </ContentPage.BindingContext> 10 11 <ContentPage.Content> 12 <StackLayout> 13 <ListView x:Name="GroupedView" 14 ItemsSource="{Binding GroupedItems}" 15 IsGroupingEnabled="true" 16 HasUnevenRows="true" 17 SeparatorVisibility="None"> 18 <ListView.GroupHeaderTemplate> 19 <DataTemplate> 20 <ViewCell Height="40"> 21 <Grid> 22 <Grid.RowDefinitions> 23 <RowDefinition /> 24 </Grid.RowDefinitions> 25 <Grid.ColumnDefinitions> 26 <ColumnDefinition /> 27 <ColumnDefinition Width="auto"/> 28 </Grid.ColumnDefinitions> 29 <Label Text="{Binding Title}" 30 VerticalTextAlignment="Center" 31 Margin="10,0,0,0"/> 32 <Button Text="Add" 33 Grid.Column="1" 34 Margin="0,0,10,0" 35 Command="{Binding AddDate}" 36 CommandParameter="{Binding Date}"/> 37 </Grid> 38 </ViewCell> 39 </DataTemplate> 40 </ListView.GroupHeaderTemplate> 41 42 〜 以下、略 〜
C#
1using Test.Models; 2using Test.Views; 3using System.Collections.Generic; 4using System.Windows.Input; 5using Test.Commands; 6 7namespace Test.ViewModels 8{ 9 public class TestViewModel : BaseViewModel 10 { 11 12 // Command処理以外、略 // 13 14 public ICommand AddDate { get; } 15 16 public TestViewModel() 17 { 18 AddDate = new AddDate(NewDate); 19 } 20 21 void NewDate() 22 { 23 //略// 24 } 25 } 26}
C#
1using System; 2using System.Windows.Input; 3 4namespace Test.Commands 5{ 6 internal class AddDate : ICommand 7 { 8 9 private readonly Action _action; 10 11 public event EventHandler CanExecuteChanged; 12 13 14 internal AddDate(Action action) 15 { 16 this._action = action; 17 } 18 19 public bool CanExecute(object parameter) 20 { 21 return true; 22 } 23 24 public void Execute(object parameter) 25 { 26 this._action(); 27 } 28 } 29} 30
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。