9
votes

Xamarin.Forms: lier à un code derrière la propriété en XAML

Dans Xamarin.Forms, je voudrais lier un code derrière une propriété à une étiquette en XAML.

J'ai trouvé de nombreuses réponses et pages Web sur ce sujet, mais elles couvrent toutes des scénarios plus complexes.

Voici ma page XAML:

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class SelViaggioPage : ContentPage
{

    private string _lblText;
    public string LblText
    {
        get
        {
            return _lblText;
        }
        set
        {
            _lblText = value;
            OnPropertyChanged();
        }
    }

    public SelViaggioPage()
    {
        InitializeComponent();
    }

    protected override void OnAppearing()
    {

        this.LblText = "Ciao!!";

        base.OnAppearing();
    }
}

Et voici le code derrière:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:TrackValigie"
             x:Class="TrackValigie.SelViaggioPage">
    <ContentPage.Content>
            <StackLayout>
                <Label Text="{Binding ?????????}" />
            </StackLayout>
    </ContentPage.Content>
</ContentPage>

Je voudrais lier le " LblText "à l'étiquette, en utilisant uniquement XAML , c'est-à-dire sans définir de contexte de liaison ou de liaison dans le code derrière.

Est-ce possible?


0 commentaires

3 Réponses :


14
votes

votre page devra implémenter INotifyPropertyChanged , mais la syntaxe de liaison doit être simplement

<ContentPage x:Name="MyPage" ... />

... 

<Label BindingContext="{x:Reference Name=MyPage}" Text="{Binding LblText}" />


2 commentaires

Merci Jason, cela a fonctionné! Je n'avais pas besoin d'implémenter INotifyPropertyChanged car ContentPage hérite de BindableObject , qui l'implémente déjà.


BindingContext = "{x: Reference MyPage}" alternativement



3
votes

Vous devez définir le x: Name pour ContentPage comme indiqué par la réponse de Jason.

 <TextBlock Text="{Binding ElementName=TestControl,Path=StudentName}"/>

Au lieu d'utiliser BindingContext, vous pouvez utiliser ElementName

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:TrackValigie"
             x:Class="TrackValigie.SelViaggioPage"
             x:Name = "MyControl"/>


1 commentaires

La syntaxe XAML Xamarin.Forms est la suivante:



0
votes

Ajoutez simplement BindingContext = this; dans le code derrière le fichier.

XAML

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class SelViaggioPage : ContentPage
{

    private string _lblText;
    public string LblText
    {
        get
        {
            return _lblText;
        }
        set
        {
            _lblText = value;
            OnPropertyChanged();
        }
    }

    public SelViaggioPage()
    {
        InitializeComponent();
        BindingContext = this;
    }

    protected override void OnAppearing()
    {
        base.OnAppearing();
        this.LblText = "Ciao!!";
    }
}

Code derrière

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:TrackValigie"
             x:Class="TrackValigie.SelViaggioPage">
    <ContentPage.Content>
            <StackLayout>
                <Label Text="{Binding LblText}" />
            </StackLayout>
    </ContentPage.Content>
</ContentPage>


0 commentaires