【发布时间】:2020-10-05 09:13:46
【问题描述】:
我在将模型对象中的属性值绑定到 XAML 中的 RelativeLayout.XConstraint 属性时遇到问题。
我有一个模型,有一个名为public int StartLocation { get; set; } 的属性,但是如果我将此属性绑定到我的RelativeLayout.XConstraint 在一个简单的BoxView 元素上,它不会设置X 约束属性。我第一次尝试这样做
<RelativeLayout BindableLayout.ItemsSource="{Binding Broadcasts}">
<BindableLayout.ItemTemplate>
<DataTemplate>
<BoxView BackgroundColor="PaleVioletRed" RelativeLayout.XConstraint="{Binding StartLocation}" WidthRequest="{Binding Length}" HeightRequest="60" />
</DataTemplate>
</BindableLayout.ItemTemplate>
</RelativeLayout>
我这里有两个绑定。 StartLocation 和 Length,以及 Length Binding 工作正常,但是使用这种方法,它将 BoxView 元素 xConstraint 设置为开始(我认为它是相对 X 位置 0)。
我也试过这种方法
<RelativeLayout BindableLayout.ItemsSource="{Binding Broadcasts}">
<BindableLayout.ItemTemplate>
<DataTemplate>
<BoxView BackgroundColor="PaleVioletRed" RelativeLayout.XConstraint="{ConstraintExpression Constant={Binding StartLocation}}" WidthRequest="{Binding Length}" HeightRequest="60" />
</DataTemplate>
</BindableLayout.ItemTemplate>
</RelativeLayout>
运行代码时出现错误提示 无法分配属性“常量”:属性不存在,或者不可分配,或者值和属性之间的类型不匹配 em>
我希望有人可以帮助我解决这个问题。提前致谢。
【问题讨论】:
-
StartLocation 设置时是否调用 OnPropertyChanged?
标签: c# xaml xamarin xamarin.forms data-binding