質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.49%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Xamarin

Xamarin(ザマリン)は、iPhoneなどのiOSやAndroidで動作し、C# 言語を用いてアプリを開発できるクロスプラットフォーム開発環境です。Xamarin Studioと C# 言語を用いて、 iOS と Android の両方の開発を行うことができます。

Q&A

解決済

2回答

1676閲覧

ListViewのGroupHeaderTemplateにButtonを追加したが、ICommandインターフェースが使えない

shi_o

総合スコア53

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Xamarin

Xamarin(ザマリン)は、iPhoneなどのiOSやAndroidで動作し、C# 言語を用いてアプリを開発できるクロスプラットフォーム開発環境です。Xamarin Studioと C# 言語を用いて、 iOS と Android の両方の開発を行うことができます。

0グッド

0クリップ

投稿2018/11/23 15:56

前提・実現したいこと

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

イメージ説明

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

AddDateはグループのクラスに持たせる必要があります。
AddDateの処理が、グループ内のアイテムを追加するというものであれば、グループのクラスで行っても問題ないと思います。

投稿2018/11/24 02:55

f-miyu

総合スコア1625

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

ベストアンサー

Command="{Binding AddDate}"
この書き方だとLabelにバインドしているTitleと同じクラスのAddDateにバインドしています。
場所を変えるか
"{Binding Source={x:Reference namedListView}, Path=DataContext.AddDate}"
Titleと同じクラスにAddDateを実装します。

投稿2018/11/24 02:44

編集2018/11/24 03:35
hihijiji

総合スコア4150

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

f-miyu

2018/11/24 03:12

Xamarinでは、RelativeSourceは使えなかったかと思います。
hihijiji

2018/11/24 03:17

指摘有難うございます。 {Binding Source={x:Reference namedListView}, ... こんな感じでしょうか
f-miyu

2018/11/24 03:29

はい。Xamarinでの同等の書き方は、そのようになります。
shi_o

2018/11/24 08:40

すいません、私素人なもので具体的にソースではどこを修正すれば良いのでしょうか・・・? ご教授お願い致します。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.49%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問