如何使本地的ContentView绑定一个Label文本?

问题描述 投票:0回答:1

我做了一个带有Expander的ContentView。我想在另一个Xaml文件中多次复制这个Xaml代码。但我希望每个Expander都有一个不同的Binding。我链接了这段代码(ContentView代码)。

<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:expandable="clr-namespace:Expandable;assembly=ExpandableView"
             x:Class="Hello.ExpandableView">
    <ContentView.Content>
        <expandable:ExpandableView Padding="5">
    <expandable:ExpandableView.PrimaryView>
        <Frame BackgroundColor="White" Padding="5" CornerRadius="10">
            <StackLayout Orientation="Horizontal">
        <Label x:Name="label" FontSize="16" TextColor="Black" FontAttributes="Bold" Padding="5" HorizontalTextAlignment="Start" HorizontalOptions="StartAndExpand"/>
        <Image Source="arrow.png" WidthRequest="25" HeightRequest="25" Margin="5"/>
                </StackLayout>
    </Frame>
    </expandable:ExpandableView.PrimaryView>
    <expandable:ExpandableView.SecondaryViewTemplate>
        <DataTemplate>
            <Frame BackgroundColor="White" Padding="5" CornerRadius="10">
        <Label Text="{Binding Tip1Uitleg}" FontSize="15" TextColor="Black" Padding="5"/>
                </Frame>
        </DataTemplate>
    </expandable:ExpandableView.SecondaryViewTemplate>
</expandable:ExpandableView>
    </ContentView.Content>
</ContentView>
namespace Hello
{
    public partial class ExpandableView : ContentView
    {
        public static BindableProperty LabelProperty =
    BindableProperty.Create(nameof(Label),
                            typeof(string),
                            typeof(ExpandableView),
                            propertyChanged: (b, o, n) => (b as ExpandableView).OnLabelChanged());

        private void OnLabelChanged()
        {
            label.Text = Label; //label is the x:Name of your Label control in ExpandableView.xaml
        }

        public string Label
        {
            get => (string)GetValue(LabelProperty);
            set => SetValue(LabelProperty, value);
        }
    }
}

连接到这个代码:

<local:ExpandableView Label="Hello"/>

但我想这样: 这不工作。

<?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:expandable="clr-namespace:Expandable;assembly=ExpandableView"
             xmlns:local="clr-namespace:Hello"
             x:Class="Hello.Health.HealthDetail"
             Title="{Binding Name}">
     <ContentPage.Content>
     <ScrollView>
        <StackLayout>
            <Image Source="{Binding Image}"/>
            <Label Text="{Binding Tip1Uitleg}" FontSize="15" TextColor="Black" Padding="10, 5, 10, 5"/>

            <local:ExpandableView Label="{Binding Tip1}"/> //This is what it is all about
</StackLayout>
            </ScrollView>
    </ContentPage.Content>
</ContentPage>

CodeBehind:

namespace Hello.Health
{
    public partial class HealthDetail : ContentPage
    {
        public HealthDetail(HealthStrings healthStrings)
        {
            if (healthStrings == null)
                throw new ArgumentNullException();

            BindingContext = healthStrings;

            InitializeComponent();


        }
    }
}

我怎么才能让这个工作?我必须使上面的这个更动态,但我不知道如何。

另外这个也能用。

<Label Text="{Binding Tip1}" />

很抱歉解释的不清楚,希望有人能帮帮我.谢谢你的时间:)

xamarin xamarin.forms binding label local
1个回答
0
投票

你不应该绑定到'this',因为你使用的是一个BindableProperty。相反,当BindableProperty的值改变时,你应该更新Label.Text属性。

public static BindableProperty LabelProperty =
    BindableProperty.Create(nameof(Label),
                            typeof(string),
                            typeof(ExpandableView),
                            propertyChanged:(b, o, n) => (b as ExpandableView).OnLabelChanged());

private void OnLabelChanged()
{
    label.Text = Label; //label is the x:Name of your Label control in ExpandableView.xaml
}

public string Label
{
    get => (string)GetValue(LabelProperty);
    set => SetValue(LabelProperty, value);
}
<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="YourProject.ExpandableView">
    <Grid>
        <Label x:Name="label"/>
    </Grid>
</ContentView>

0
投票

通常情况下,当你想为binableproperty绑定一个值时,你可以尝试下面的代码。

ExpandableView: 我做了一个简单的contentview,同样是binableproperty,供大家参考。

<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
x:Class="XamarinDemo.Pages.ExpandableView">
   <ContentView.Content>
       <StackLayout>
          <Label Text="Hello"></Label>
          <Label x:Name="label"></Label>
       </StackLayout>
   </ContentView.Content>
</ContentView>

后面的代码。

public partial class ExpandableView : ContentView
 {
    public static BindableProperty LabelProperty =
 BindableProperty.Create(nameof(Label),
                        typeof(string),
                        typeof(ExpandableView),
                        propertyChanged: (b, o, n) => (b as 
ExpandableView).OnLabelChanged());

    private void OnLabelChanged()
    {
        label.Text = Label; //label is the x:Name of your Label control in ExpandableView.xaml
    }

    public string Label
    {
        get => (string)GetValue(LabelProperty);
        set => SetValue(LabelProperty, value);
    }
    public ExpandableView()
    {
        InitializeComponent();
    }

 }

使用方法:

 <ContentPage.Content>
    <local:ExpandableView Label="{Binding str}"></local:ExpandableView>
</ContentPage.Content>

后面的代码:

public partial class Page1 : ContentPage
  {
    public string str { get; set; }
    public Page1()
    {
        InitializeComponent();

        str = "okay";

        this.BindingContext = this;
     }
 }

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.