Skip to main content
Edit after comment
Source Link
galex
  • 564
  • 4
  • 20

Update after comment

You have to tell MyView.xaml where to search for Data binding. In case you place your view in a namespace named controls

namespace MinimalNotWorkingExample;

public partial class MyView : ContentView
{
   public class CustomClass
   {
      public String Id { get; set; }
      public String Name { get; set; }
   }

   public static readonly BindableProperty DataProperty =
      BindableProperty.Create(
         propertyName: nameof(Data),
         returnType: typeof(CustomClass),
         declaringType: typeof(MyView),
         defaultValue: default(CustomClass));

   public CustomClass Data
   {
      get { return (CustomClass)GetValue(DataProperty); }
      set { SetValue(DataProperty, value); }
   }
   
   public MyView()
   {
      InitializeComponent();
   }
}
<?xml version="1.0" encoding="utf-8" ?>
<ContentView xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:controls="clr-namespace:MinimalNotWorkingExample.Controls;assembly=MinimalNotWorkingExample"
             x:Class="MinimalNotWorkingExample.MyView">
    <VerticalStackLayout>
        <Label 
            Text="If you can Read this, the ContentView was loaded. IF you can see the same text below as in the header, the Binding is working."
            VerticalOptions="Center" 
            HorizontalOptions="Center"
            TextColor="Black"/>
        <Label 
            Text="{Binding Source={RelativeSource AncestorType={x:Type controls:MyView}}, Path=Data.Name, x:DataType=controls:MyView}"
            VerticalOptions="Center" 
            HorizontalOptions="Center"
            TextColor="Black"/>
    </VerticalStackLayout>
</ContentView>

You should not set your BindingContext like you are doing:


public partial class MyView : ContentView
{
    public MyView()
    {
        //-> BindingContext = this;
        InitializeComponent();
    }
}

You have to tell MyView.xaml where to search for Data binding. In case you place your view in a namespace named controls

<?xml version="1.0" encoding="utf-8" ?>
<ContentView xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:controls="clr-namespace:MinimalNotWorkingExample.Controls;assembly=MinimalNotWorkingExample"
             x:Class="MinimalNotWorkingExample.MyView">
    <VerticalStackLayout>
        <Label 
            Text="If you can Read this, the ContentView was loaded. IF you can see the same text below as in the header, the Binding is working."
            VerticalOptions="Center" 
            HorizontalOptions="Center"
            TextColor="Black"/>
        <Label 
            Text="{Binding Source={RelativeSource AncestorType={x:Type controls:MyView}}, Path=Data, x:DataType=controls:MyView}"
            VerticalOptions="Center" 
            HorizontalOptions="Center"
            TextColor="Black"/>
    </VerticalStackLayout>
</ContentView>

You should not set your BindingContext like you are doing:


public partial class MyView : ContentView
{
    public MyView()
    {
        //-> BindingContext = this;
        InitializeComponent();
    }
}

Update after comment

You have to tell MyView.xaml where to search for Data binding. In case you place your view in a namespace named controls

namespace MinimalNotWorkingExample;

public partial class MyView : ContentView
{
   public class CustomClass
   {
      public String Id { get; set; }
      public String Name { get; set; }
   }

   public static readonly BindableProperty DataProperty =
      BindableProperty.Create(
         propertyName: nameof(Data),
         returnType: typeof(CustomClass),
         declaringType: typeof(MyView),
         defaultValue: default(CustomClass));

   public CustomClass Data
   {
      get { return (CustomClass)GetValue(DataProperty); }
      set { SetValue(DataProperty, value); }
   }
   
   public MyView()
   {
      InitializeComponent();
   }
}
<?xml version="1.0" encoding="utf-8" ?>
<ContentView xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:controls="clr-namespace:MinimalNotWorkingExample.Controls;assembly=MinimalNotWorkingExample"
             x:Class="MinimalNotWorkingExample.MyView">
    <VerticalStackLayout>
        <Label 
            Text="If you can Read this, the ContentView was loaded. IF you can see the same text below as in the header, the Binding is working."
            VerticalOptions="Center" 
            HorizontalOptions="Center"
            TextColor="Black"/>
        <Label 
            Text="{Binding Source={RelativeSource AncestorType={x:Type controls:MyView}}, Path=Data.Name, x:DataType=controls:MyView}"
            VerticalOptions="Center" 
            HorizontalOptions="Center"
            TextColor="Black"/>
    </VerticalStackLayout>
</ContentView>
added 9 characters in body
Source Link
galex
  • 564
  • 4
  • 20

You have to tell MyView.xaml where to search for Data binding. In case you place your view in a namespace named controls

<?xml version="1.0" encoding="utf-8" ?>
<ContentView xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:controls="clr-namespace:MinimalNotWorkingExample.Controls;assembly=MinimalNotWorkingExample"
             x:Class="MinimalNotWorkingExample.MyView">
    <VerticalStackLayout>
        <Label 
            Text="If you can Read this, the ContentView was loaded. IF you can see the same text below as in the header, the Binding is working."
            VerticalOptions="Center" 
            HorizontalOptions="Center"
            TextColor="Black"/>
        <Label 
            Text="{Binding Source={RelativeSource AncestorType={x:Type controls:MyView}}, Path=Data, x:DataType=controls:MyView}"
            VerticalOptions="Center" 
            HorizontalOptions="Center"
            TextColor="Black"/>
    </VerticalStackLayout>
</ContentView>

You should not set your bindingBindingContext like you are doing:


public partial class MyView : ContentView
{
    public MyView()
    {
        //-> BindingContext = this;
        InitializeComponent();
    }
}

You have to tell MyView.xaml where to search for Data binding. In case you place your view in a namespace named controls

<?xml version="1.0" encoding="utf-8" ?>
<ContentView xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:controls="clr-namespace:MinimalNotWorkingExample.Controls;assembly=MinimalNotWorkingExample"
             x:Class="MinimalNotWorkingExample.MyView">
    <VerticalStackLayout>
        <Label 
            Text="If you can Read this, the ContentView was loaded. IF you can see the same text below as in the header, the Binding is working."
            VerticalOptions="Center" 
            HorizontalOptions="Center"
            TextColor="Black"/>
        <Label 
            Text="{Binding Source={RelativeSource AncestorType={x:Type controls:MyView}}, Path=Data, x:DataType=controls:MyView}"
            VerticalOptions="Center" 
            HorizontalOptions="Center"
            TextColor="Black"/>
    </VerticalStackLayout>
</ContentView>

You should not set your binding like you are doing:


public partial class MyView : ContentView
{
    public MyView()
    {
        //-> BindingContext = this;
        InitializeComponent();
    }
}

You have to tell MyView.xaml where to search for Data binding. In case you place your view in a namespace named controls

<?xml version="1.0" encoding="utf-8" ?>
<ContentView xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:controls="clr-namespace:MinimalNotWorkingExample.Controls;assembly=MinimalNotWorkingExample"
             x:Class="MinimalNotWorkingExample.MyView">
    <VerticalStackLayout>
        <Label 
            Text="If you can Read this, the ContentView was loaded. IF you can see the same text below as in the header, the Binding is working."
            VerticalOptions="Center" 
            HorizontalOptions="Center"
            TextColor="Black"/>
        <Label 
            Text="{Binding Source={RelativeSource AncestorType={x:Type controls:MyView}}, Path=Data, x:DataType=controls:MyView}"
            VerticalOptions="Center" 
            HorizontalOptions="Center"
            TextColor="Black"/>
    </VerticalStackLayout>
</ContentView>

You should not set your BindingContext like you are doing:


public partial class MyView : ContentView
{
    public MyView()
    {
        //-> BindingContext = this;
        InitializeComponent();
    }
}

Source Link
galex
  • 564
  • 4
  • 20

You have to tell MyView.xaml where to search for Data binding. In case you place your view in a namespace named controls

<?xml version="1.0" encoding="utf-8" ?>
<ContentView xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:controls="clr-namespace:MinimalNotWorkingExample.Controls;assembly=MinimalNotWorkingExample"
             x:Class="MinimalNotWorkingExample.MyView">
    <VerticalStackLayout>
        <Label 
            Text="If you can Read this, the ContentView was loaded. IF you can see the same text below as in the header, the Binding is working."
            VerticalOptions="Center" 
            HorizontalOptions="Center"
            TextColor="Black"/>
        <Label 
            Text="{Binding Source={RelativeSource AncestorType={x:Type controls:MyView}}, Path=Data, x:DataType=controls:MyView}"
            VerticalOptions="Center" 
            HorizontalOptions="Center"
            TextColor="Black"/>
    </VerticalStackLayout>
</ContentView>

You should not set your binding like you are doing:


public partial class MyView : ContentView
{
    public MyView()
    {
        //-> BindingContext = this;
        InitializeComponent();
    }
}