1

I have a small MAUI program using CollectionView - and per item, there are buttons attached with a command (like changeitem)

The app is working. But I am getting warnings at compile and runtime about data binding.

            <CollectionView.ItemTemplate>
            <DataTemplate x:DataType="model:CheckItem">
                <Grid ColumnDefinitions="*,Auto" ColumnSpacing="{StaticResource StdSpacing}">
                    <Grid
                        Grid.Column="1"
                        Margin="{StaticResource StdMargin}"
                        ColumnDefinitions="auto,auto"
                        ColumnSpacing="{StaticResource StdSpacing}">
                        <Button
                            Grid.Column="0"
                            Command="{Binding Source={RelativeSource AncestorType={x:Type viewmodel:OverviewViewModel}}, Path=ChangeItemCommand}"
                            CommandParameter="{Binding .}"
                            Style="{x:StaticResource GoogleFont}"
                            Text="{x:Static font:GoogleIconFont.Edit}" />
                    </Grid>
                </Grid>
            </DataTemplate>
        </CollectionView.ItemTemplate>

The commands are located in the ViewModel viewmodel:OverviewViewModel whereas the data model for an item is located in model:CheckItem.

The ContenPage starts with

x:Class="BicycleCheckList.OverviewPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:font="clr-namespace:BicycleCheckList.Utils"
xmlns:local="clr-namespace:BicycleCheckList.Resources.Strings"
xmlns:model="clr-namespace:BicycleCheckList.Models"
xmlns:utils="clr-namespace:BicycleCheckList.Utils"
xmlns:viewmodel="clr-namespace:BicycleCheckList.ViewModels"
Title="{Binding Title}"
x:DataType="viewmodel:OverviewViewModel">

On Compile time, I am getting a warning: XC0045 Binding: Property "ChangeItemCommand" not found on "BicycleCheckList.Models.CheckItem".

The binding of the command Command="{Binding Source={RelativeSource AncestorType={x:Type viewmodel:OverviewViewModel}}, Path=ChangeItemCommand}" does not find the command.

Why?

The (toy) app is also available on Github

3
  • How can you set the Datatype on the binding directly? Command="{Binding Source={RelativeSource AncestorType={x:Type viewmodel:OverviewViewModel}}, Path=ChangeItemCommand}" Setting the data type on the button itself silents the compile time warning. But then when I want to change the item with the button, I am getting a runtime error, since the command's paramter is now null Commented Nov 17, 2024 at 13:32
  • Literally by specifying x:DataType=viewmodel:OverviewViewModel as a property: Command="{Binding Source={RelativeSource AncestorType={x:Type viewmodel:OverviewViewModel}}, Path=ChangeItemCommand, x:DataType=viewmodel:OverviewViewModel}" Commented Nov 17, 2024 at 13:33
  • I've deleted my earlier comments as they were based on not understanding the two modes on offer. I'm delighted to say this question didn't just help solve your problem, but mine too :) Commented Nov 17, 2024 at 13:40

1 Answer 1

0

While it looks somewhat redundant, you can specify the x:DataType of the binding. I believe all you need to do is change your Button declaration to:

<Button
    Grid.Column="0"
    Command="{Binding x:DataType=viewmodel:OverviewViewModel, Source={RelativeSource AncestorType={x:Type viewmodel:OverviewViewModel}}, Path=ChangeItemCommand}"
    CommandParameter="{Binding .}"
    Style="{x:StaticResource GoogleFont}"
    Text="{x:Static font:GoogleIconFont.Edit}" />

(I would normally put x:DataType at the end, but then it wouldn't be visible without scrolling...)

Sign up to request clarification or add additional context in comments.

2 Comments

Command="{Binding Source={RelativeSource AncestorType={x:Type viewmodel:OverviewViewModel}}, Path=ChangeItemCommand, x:DataType=viewmodel:OverviewViewModel}" is the answer. Man ..., it is so simple. I always put the data type outside the the quotes. That was wrong.
And another hint. There is no code completion in XAML VS 2022 for the x:DataType in a Binding.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.