Xamarin.Forms XAML中绑定父属性和子属性

3
我已经搜索了两天,但无法找到这个问题的答案。我有一个简单的内容页面,其中包含一个可绑定属性:
public partial class Page1 : ContentPage
{
    public static readonly BindableProperty MainTextProperty = BindableProperty.Create<Page1 , string>(p => p.MainText, string.Empty);

    public string MainText
    {
        get
        {
            return (string)GetValue(MainTextProperty);
        }
        set
        {
            SetValue(MainTextProperty, value);
        }
    }
}

在XAML中,我有以下代码:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="Test.Pages.Page1">
<Grid HorizontalOptions="Fill" VerticalOptions="Fill">
<Grid.RowDefinitions>
  <RowDefinition Height="*" />
  <RowDefinition Height="*" />
  <RowDefinition Height="*" />
  <RowDefinition Height="*" />
</Grid.RowDefinitions>

<Label Text="{Binding MainText}" Grid.Row="0"/>    
<Label Text="{Binding MainText}" Grid.Row="1"/>
<Label Text="{Binding MainText}" Grid.Row="2"/>
<ctrl:CustomControl CustomProperty="{Binding MainText}" />
<Label Text="{Binding MainText}" Grid.Row="3"/>
</Grid>
</ContentPage>

在自定义控件中,类似这样的东西:
<StackLayout>
   <Label x:Name="lbl1" />
</StackLayout>

在代码后台中,我使用OnPropertyChanged将接收到的值设置为CustomProperty和lbl1之间的值。 CustomProperty的定义方式与我定义MainTextProperty的方式相同。
然而,这并不起作用。我寻找任何可以让我做类似事情的示例,但我没有找到。你们中的任何人知道我如何完成这样的事情吗?我正在接收类型不匹配的问题,即我传递的值与Xamarin.Forms.Binding之间的值不匹配。
3个回答

1
另一种方法是使用x:Reference。在这种情况下,您不需要更新页面的数据上下文。
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="App1.Page1"
             x:Name="root">
  <Grid HorizontalOptions="Fill" VerticalOptions="Fill">
    <Grid.RowDefinitions>
      <RowDefinition Height="*" />
      <RowDefinition Height="*" />
      <RowDefinition Height="*" />
      <RowDefinition Height="*" />
    </Grid.RowDefinitions>

    <Label Text="{Binding MainText}" Grid.Row="0"/>
    <Label Text="{Binding MainText}" Grid.Row="1"/>
    <Label Text="{Binding MainText}" Grid.Row="2" BindingContext="{x:Reference Name=root}"/>
    <Label Text="{Binding MainText}" Grid.Row="3"/>
  </Grid>
</ContentPage>

我按照你说的添加了绑定上下文,但是我收到了类型不匹配的异常。 - Ubler
我在我的示例项目中测试了这个XAML,一切正常。 在此您可以找到更多关于Xamarin.Forms中视图到视图绑定的信息 http://developer.xamarin.com/guides/cross-platform/xamarin-forms/xaml-for-xamarin-forms/data_binding_basics/ 您能否附上您的示例项目? - Artur Shamsutdinov

0

你是否忘记添加BindingContext了?我尝试了下面的代码,它可以正常工作。

    public Page1 ()
    {
        InitializeComponent ();

        BindingContext = this;
        MainText = "123";
    }

我看到你的类被命名为Page1,那么在BindableProperty中的Declarer也应该是Page1而不是HomePage

public static readonly BindableProperty MainTextProperty = BindableProperty.Create<Page1, string>(p => p.MainText, string.Empty);

0
我在这个问题上挣扎了一段时间,发现绑定的返回类型不是字符串,而应该是Binding。顺便提一下,具有通用类型的BindableProperties已经过时了,所以你的代码应该像这样:
public partial class Page1 : ContentPage
{
    public static readonly BindableProperty MainTextProperty = BindableProperty.Create("MainText",typeof(Binding), typeof(Page1),null);

    public Binding MainText
    {
        get
        {
            return (Binding)GetValue(MainTextProperty);
        }
        set
        {
            SetValue(MainTextProperty, value);
        }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接