0

I simplify everything to better focus the problem

this is the page that displays the cardview

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:controls="clr-namespace:TestCard"
    x:Class="TestCard.MainPage"
    Title="Test Card">
    <ContentPage.Resources>
        <ControlTemplate x:Key="TestCardView">
            <Label x:Name="lblTitolo" Text="{TemplateBinding TitoloCardView}" />
        </ControlTemplate>
    </ContentPage.Resources>
    
    <VerticalStackLayout BindingContext="{Binding Source={RelativeSource TemplatedParent}}">
        <controls:ControlCardViews x:Name="ctrlName01" TitoloCardView="Questo è il primo Titolo"
            ControlTemplate="{StaticResource TestCardView}" />
        <controls:ControlCardViews x:Name="ctrlName02" TitoloCardView="Secondo Titolo" 
            ControlTemplate="{StaticResource TestCardView}" />
    </VerticalStackLayout>
</ContentPage>

here i check the cardview data

namespace TestCard;

public class ControlCardViews : ContentView
{
    public static readonly BindableProperty TitoloCardViewProperty = BindableProperty.Create(nameof(TitoloCardView), typeof(string),
    typeof(ControlCardViews), string.Empty);
    public string TitoloCardView
    {
        get => (string)GetValue(TitoloCardViewProperty);
        set => SetValue(TitoloCardViewProperty, value);
    }
}

I would like to set/modify the text of the "TitoloCardView" not in the xaml file but as a C# code

I would also like to create this part

<controls:ControlCardViews x:Name="ctrlName01" TitoloCardView="Questo è il primo Titolo"
    ControlTemplate="{StaticResource TestCardView}" />
<controls:ControlCardViews x:Name="ctrlName02" TitoloCardView="Secondo Titolo" 
    ControlTemplate="{StaticResource TestCardView}" />

not as xaml code but C# code thanks

I tried to google it but I couldn't find any idea on how to fix it

4
  • Your question suggests that you need to take up an introduction to MAUI and MVVM. Commented Aug 21, 2024 at 9:57
  • <controls:ControlCardViews ..../> is in MainPage.xaml. Do you mean you want to implement this code in MainPage.xaml.cs? Commented Aug 21, 2024 at 10:00
  • @StephenQuan ok, I hope I can find the solution with your clue Commented Aug 21, 2024 at 11:26
  • @AntonioRibaudo The example of control templates and card view are not very good connected to each other, and are meant more for a demonstration of a principle, than for actual example how to use them in a project. I suggest some changes, for how to approach templating your controls. This is how I use it in production. For making custom controls reusable and to keep the pages xamls as clean as possible. Commented Aug 21, 2024 at 13:35

1 Answer 1

0

This is what I recommend as a fix-up.

  1. In your Project > Resource > Styles, create new ResourceDictionary file called ControlTemplates.xaml, and place your Control Template there.

BindingContext="{Binding Source={RelativeSource TemplatedParent}}"

is applied to your control template.

  1. Binding instead of TemplateBinding, you use Binding in your control template.

So it should look close to this:

<ControlTemplate x:Key="MyTemplate"..
   <Grid BindingContext="{Binding Source={RelativeSource TemplatedParent}}"...
        <SomeControl Something="{Binding Something}"...
        <AnotherControl SomethingElse="{Binding SomethingElse}"...
  1. In your App.xaml, you add the Resource Dictionary like that:

    <ResourceDictionary Source="Resources/Styles/ControlTemplates.xaml"/>
    
  2. In your page XAML, when you use the ControlTemplate, you DO NOT specify binding context. At the left part, you have your ContentView, holding your bindable properties. At the right part you specify your ControlTemplate, so it knows how those properties actually render on the screen, and to what specific controls they bind. This is enough.

Done. Now you can change those properties in code, and the UI will respond.

(MVVM is another topic, I will leave it for now, because what I said is confusing enough.)

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

Comments

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.