Search code examples
c#xamlmauiwildcard

Modify a data of a cardview through c# code


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


Solution

  • 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.)