実現したいこと
Mauiでxamlを記述している
BindingContext DataType関連の警告を消したい
前提
VisualstudioでMauiを使いC#でMVVM方式でアプリの開発をしている
大方、エラーも無くなり、稼働はできているものの、警告が消せないものがあり解決したい
BindingContext DataType関連だと思うが以下の警告がでる
発生している問題・警告メッセージ
Binding: Property "TestTapCommand" not found on "InspectApp.ViewModels.InspectItems".
発生しているXAMLバインドエラ―
Mismatch between the specified x:DataType (InspectApp.ViewModels.InspectItems) and the current binding context (Microsoft.Maui.Controls.RelativeBindingSource).
該当のソースコード
xaml
1<?xml version="1.0" encoding="utf-8" ?> 2<ContentPage 3 x:Class="SpecifyInspectApp.Views.UserCheckPage" 4 xmlns="http://schemas.microsoft.com/dotnet/2021/maui" 5 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 6 xmlns:local="clr-namespace:SpecifyInspectApp.ViewModels" 7 xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit" 8 x:Name="MyPage" 9 x:DataType="local:UserCheckPageModel"> 10 11 <ContentPage.BindingContext> 12 <local:UserCheckPageModel /> 13 </ContentPage.BindingContext> 14 15 <ContentPage.Behaviors> 16 <toolkit:EventToCommandBehavior Command="{Binding NavigatedToCommand}" EventName="NavigatedTo" /> 17 <toolkit:EventToCommandBehavior Command="{Binding NavigatedFromCommand}" EventName="NavigatedFrom" /> 18 </ContentPage.Behaviors> 19 20 <ContentPage.Resources> 21 <ResourceDictionary> 22 ***省略*** 23 </ResourceDictionary> 24 </ContentPage.Resources> 25 26 <Grid> 27 <Grid RowDefinitions="Auto,Auto,Auto,*"> 28 ***省略--- 29 <Border 30 Grid.Row="3" 31 Padding="3" 32 Stroke="Navy" 33 StrokeThickness="2"> 34 <Grid> 35 <CollectionView 36 x:Name="collectionView" 37 EmptyView="No Data" 38 ItemsSource="{Binding DisplayList}" 39 ItemsUpdatingScrollMode="KeepScrollOffset" 40 SelectionMode="Single"> 41 <CollectionView.ItemTemplate> 42 <DataTemplate x:DataType="local:InspectItems"> 43 <Grid RowDefinitions="Auto,3"> 44 <Grid Grid.Row="0" ColumnDefinitions="190,80,90,80,150,*"> 45 <!-- 各セルを定義します --> 46 <Label 47 Grid.Column="0" 48 Style="{StaticResource ResultDataStyle}" 49 Text="{Binding PartNo}" /> 50 <Label 51 Grid.Column="1" 52 HorizontalTextAlignment="End" 53 Style="{StaticResource ResultDataStyle}" 54 Text="{Binding Qty}" /> 55 <Label 56 Grid.Column="2" 57 HorizontalTextAlignment="Center" 58 Style="{StaticResource ResultDataStyle}" 59 Text="{Binding ShipC}" /> 60 <Label 61 Grid.Column="3" 62 HorizontalTextAlignment="Center" 63 Style="{StaticResource ResultDataStyle}" 64 Text="{Binding ShipDate}" /> 65 <Label 66 Grid.Column="4" 67 Style="{StaticResource ResultDataStyle}" 68 Text="{Binding Slip}" /> 69 <Label 70 Grid.Column="5" 71 Style="{StaticResource ResultDataStyle}" 72 Text="{Binding DlvNo}" /> 73 <!-- 他の列も追加します --> 74 <Grid.GestureRecognizers> 75 <TapGestureRecognizer 76 Command="{Binding Source={RelativeSource AncestorType={x:Type local:UserCheckPageModel}}, Path=TestTapCommand}" 77 CommandParameter="{Binding .}" 78 NumberOfTapsRequired="2" /> 79 </Grid.GestureRecognizers> 80 </Grid> 81 <BoxView 82 Grid.Row="1" 83 BackgroundColor="Navy" 84 HeightRequest="1" 85 VerticalOptions="Center" /> 86 </Grid> 87 </DataTemplate> 88 </CollectionView.ItemTemplate> 89 </CollectionView> 90 </Grid> 91 </Border> 92 </Grid> 93 94 </Grid> 95</ContentPage>
C#
1namespace InspectApp.ViewModels 2{ 3 public partial class UserCheckPageModel : ObservableObject, IDisposable 4 { 5 /// <summary> 6 /// 表示用デ-タ 7 /// </summary> 8 public ObservableCollection<InspectItems> DisplayList { get; set; } = []; 9 10 } 11 12 public class InspectItems 13 { 14 public string ShipDate { get; set; } = "1/1"; 15 public string PartNo { get; set; } = "XZXZY-AAAA"; 16 public string Qty { get; set; } = "1"; 17 public string ShipC { get; set; } = "TOKYO"; 18 public string DlvNo { get; set; } = "372451_FFF"; 19 public string Slip { get; set; } = "372451"; 20 } 21} 22
試したこと
CollectionViewで対象のセルをダブルタップ・クリックしてデータを取得するコマンドを作りたい
<DataTemplate x:DataType="local:InspectItems">
で再度宣言しているのでこれが原因とは思っている
しかし
<DataTemplate x:DataType="local:InspectItems">
を宣言しないとBinding PartNoなどが全く反映されない上、別の警告が大量に発生する
ネット上にも有益な情報はなく
Muaiではこの警告は仕方がないものなのか、ほかに消す方法があるのか知りたい
※一応、データの表示、ダブルタップでの動作は検証成功している
回答1件
あなたの回答
tips
プレビュー